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.
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.
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.
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)
test "should not save user without username"doassert_raises(NameError)do user = User.new user.save endend
Now to run our tests:
现在运行我们的测试:
rails testtest/models/user_test.rb
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!
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).
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”.
test "should get index"doget 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:
你会看见:
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