上银导轨模型_导轨测试变得简单

本文介绍如何为Rails应用程序或API创建测试,包括模型和控制器的测试方法。通过使用assert_raises等方法,可以确保模型验证有效,而通过模拟HTTP请求,可以测试控制器的功能。文章提供了详细的步骤和示例代码。
摘要由CSDN通过智能技术生成

上银导轨模型

Creating tests for your Rails app or API is a critical step in creating a production-level application. If you’re new to Rails, testing may seem esoteric and time-consuming.

为Rails应用程序或API创建测试是创建生产级应用程序的关键步骤。 如果您是Rails的新手,那么测试似乎很深奥且耗时。

As it turns out, it’s actually not difficult to get started and testing will save you a ton of time. Once your back-end is iron clad and easily testable your front-end work will be smooth as butter.

事实证明,开始实际上并不难,测试将为您节省大量时间。 一旦您的后端被铁覆盖并易于测试,您的前端工作将像黄油一样光滑。

For detailed info here are the docs: https://guides.rubyonrails.org/testing.html

有关详细信息,请参见以下文档: https : //guides.rubyonrails.org/testing.html

If you are using Rails Generator to generate your controllers and models. There should be a “test” folder in the root of the application. Inside, the generator will generate files for you corresponding with the controllers and models you are generating.

如果使用Rails Generator生成控制器和模型。 在应用程序的根目录中应该有一个“ test”文件夹。 在内部,生成器将为您生成与要生成的控制器和模型相对应的文件。

测试模型 (Testing for Models)

Let’s write some simple tests for the User model.

让我们为User模型编写一些简单的测试。

Hop into the user_test.rb file in the test/models folder. Now let’s write some code.

跳到test / models文件夹中的user_test.rb文件。 现在让我们编写一些代码。

So we will use the assert_raises method to check if this raises an error or not. If it does it will give us the green! If not it will be red red red! See, testing made simple. (there are many assertions we can use to test, an assertion is just a check)

因此,我们将使用assert_raises方法检查这是否引发错误。 如果这样做,将会给我们绿色! 如果不是,它将是红色红色红色! 看,测试变得简单。 (有很多断言可以用来测试,断言只是一个检查)

test "should not save user without username" do   assert_raises(NameError) do     user = User.new     user.save   endend

Now to run our tests:

现在运行我们的测试:

rails test test/models/user_test.rb
Image for post

As you can see our test gave us no failures and a little green dot. This is good! That means trying to save a User without any username or password is throwing an error. If you are throwing an error, make sure you have some validations in your darn User model!

如您所见,我们的测试没有给我们带来任何失败,并且给了我们一点绿色。 很好! 这意味着试图保存没有任何用户名或密码的用户会引发错误。 如果抛出错误,请确保您的织补用户模型中有一些验证!

测试控制器 (Testing for Controllers)

Let’s write some tests for our controllers.

让我们为控制器编写一些测试。

First, let’s test out our index function. In a normal application you may or may not want to allow a “get” all of your users. But for the purposes of this demo we’ll assume that we can.

首先,让我们测试一下索引功能。 在普通应用程序中,您可能会或可能不希望允许“获取”所有用户。 但是出于演示目的,我们假设可以。

First, you should see that there is a UsersControllerTest already set up in your test/models folder. (If you don’t you need to “rails g” a user controller).

首先,您应该看到在test / models文件夹中已经设置了一个UsersControllerTest。 (如果不这样做,则需要“限制”用户控制器)。

Now, we have access to a few functions that will allow us to create faux HTTP requests.

现在,我们可以访问一些函数,这些函数将允许我们创建伪HTTP请求。

  • get

    get

  • post

    post

  • patch

    patch

  • put

    put

  • head

    head

  • delete

    delete

So for this situation we want to create a “get” to “/users”. We can do that two ways; with a string or with the users_url helper. I will do it with the helper here. But keep in mind this can be replaced with “/users”.

因此,对于这种情况,我们想为“ / users”创建一个“ get”。 我们可以通过两种方式做到这一点; 使用字符串或users_url帮助器。 我将在这里与助手一起做。 但是请记住,可以将其替换为“ / users”。

test "should get index" do   get users_url # could be "/users"   assert_response :successend

Now, to run tests, just use one of the following 3 commands:

现在,要运行测试,只需使用以下三个命令之一:

## to run all tests:rails test## run all tests in the user_controller_test filerails test test/controllers/user_controller_test.rb## to run a specific testrails test test/controllers/user_controller_test.rb:7## 7 being the line that that function method begins

You will see:

你会看见:

Image for post

This means the test has passed. If you see a lot of red and an error or a failure… well that means there was an error thrown or the test failed.

这意味着测试已通过。 如果您看到很多红色,并且有错误或失败…那么这意味着抛出错误或测试失败。

Each green dot represents a test passed!

每个绿色的点代表一个测试通过!

更多信息 (More Info)

This is just the tip of the iceberg when it comes to testing. Hopefully these basic methods get you started when it comes to testing.

这只是测试方面的冰山一角。 希望这些基本方法可以帮助您入门进行测试。

For more information please visit the Ruby Docs. This section is particularly well written:

有关更多信息,请访问Ruby Docs。 本节的写法特别出色:

Thank you for reading, please follow me on Twitter or IG @thedrewprint and connect with me on LinkedIn — Andrew Richards

感谢您的阅读,请在Twitter或IG @thedrewprint上关注我,并在LinkedIn上与我联系— Andrew Richards

翻译自: https://medium.com/swlh/testing-in-rails-made-simple-83cc38abd7f5

上银导轨模型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值