Today to introduce some of the assorted testing knowledge, but they can not be missed.
First, let's introduce the common test commands
Rake Test
Run all tests, including unit tests, functional tests, and integration tests.
Rake Test:units
Run all the unit tests.
Rake Test:functionals
Run all the functional tests
Rake Test:integration
Run all the integration tests.
Rake Test:recent
Run the recently modified test.
Rake test:uncommited
Run any uncommitted tests. Support for SVN and git.
Setup and teardown
In a test class that contains multiple test methods, we might include some code that needs to be executed before and after each test run.
such as creating objects, cleaning objects, and so on. It is convenient for maintenance, easy to modify and easy to read.
We can give it to setup and teardown to do it. The setup is that the code in is run before each test method executes, and the code in teardown runs after each test method executes.
Here we take an example of a previous functional test for Admin::tagscontroller, introducing setup and teardown. Add tag is required to login, will use the user information, need to have a value in the session, this part of the Admin::tagscontroller in all the action is needed, and use the same can be, put this part in the setup and teardown is appropriate.
Require ' Test_helper '
class Admin::tagscontrollertest < Actioncontroller::testcase
include Factorygirl:: Syntax::methods
def setup
@user_valid = Create (: user_valid)
@request. session[:user_id] = @user_valid. ID
End
def teardown
@user_valid = nil
end
def test_should_create_tag_successfully
tag = Build (: Tag_valid)
assert_difference "Tag.count" do
post:create, {: Tag => {: Title => Tag.title}}
End
-def test_should_create_tag_fail
assert_no_difference "Tag.count" do
post:create, {: Tag => {: Title => ""}} end end
and rails also implements setup and teardown as callback so you can specify setup and teardown in the following ways.
Block A
A method
A method name as a symbol
A lambda
Setup:init
teardown do
@user_valid = nil
end
private
def init
@user_valid = Create (: user_ Valid)
@request. session[:user_id] = @user_valid. ID
End
Test routes
Assert_routing (
"SignOut",
{: Controller => "Sessions",: Action => "Destroy": Method =>:d Elete}
)
def test_route_posts_id
category = Factorygirl.create (: category_valid)
article = factorygirl.create (:p ost _valid)
assert_routing ("Posts/#{article.id}", {: Controller => " posts",: Action => "Show",: ID => article.id.to_s}) End
This article is from the "It architects in the breakout" blog, please be sure to keep this source http://virusswb.blog.51cto.com/115214/1079484
See more highlights of this column: http://www.bianceng.cn/Programming/project/