As we've mentioned before, the rails functional test is for controller, testing whether the controller action is performing correctly.
The main contents of the test are:
The test request is correct.
Test whether the user jumps to the correct page.
Test whether the user verified success.
The correct object is included in the test response.
Test whether the appropriate information is displayed to the user in view.
Common assertion functions
1.assert_response (type, message = nil)
Asserts whether the response was successful or not the specified status code.
Assert_response:success on behalf of 200,assert_response:redirect representative 300-399,assert_response:missing 404,assert_response: The error stands for 500-599.
Assert_response:success
Assert_response (200)
2.assert_redirected_to (options = {}, Message=nil)
Verify that the jump is correct and jumps to the specified controller action.
Assert_redirected_to (: Controller => "Posts",: Action => "index")
Assert_redirected_to (: Controller => "Posts",: Action => "show")
3.assert_template (expected = nil, message=nil)
Asserts whether the request renders the correct page.
Assert_template "New"
Whether the new page is rendered. Assert_template:p artial => ' _customer ',: Locals => {: Customer => @customer}
Has the customer this partial been rendered and the variable is loaded @customer
Assert_template:p artial => false does not render any partial.
4.assert_tag
The tag label for the specified condition in the body of the assertion response.
Assert_tag:tag => "div": attributes => {: id => "Div-header"} verifies that the body of the response contains a div tag for Id=d "Iv-header".
5.assert_no_tag
In contrast to Assert_tag, asserts that the body of the response does not contain the tag for the specified condition.
6.assert_routing
Asserts that the route mapping is correct.
def Test_should_route_from_signout_to_destroy
assert_routing ({:p ath => "SignOut": Method =>:d Elete}, {: Controller => "Sessions",: Action => "Destroy",: Method =>:d elete}) End
7.assert_select
Asserts the tag in view.
Assert_select "Form"
Assert_select "title", "Welcome to my site!"
Example
Let's take my blog project as an example.
Here are two simple tests for Sessionscontroller's new action, testing whether the action returned successfully and loading the variable user.
Require ' Test_helper '
class Sessionscontrollertest < Actioncontroller::testcase
include Factorygirl:: Syntax::methods
def test_should_be_get_new
get:new
assert_response:success
Assert_not_nil Assigns (@user)
end
def test_should_be_get_new_status_code
get:new
assert_response (200)
Assert_not_nil assigns (: User)
End
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/