之前我们介绍过,rails的功能测试针对的是controller,测试controller的action是否正确的执行。

测试的内容主要是:

  • 测试请求是否正确。
  • 测试用户是否跳转到正确的页面。
  • 测试用户是否验证成功。
  • 测试响应中是否包含了正确的对象。
  • 测试在view中是否给用户显示了适当的信息。

 

常用的断言函数

1.assert_response(type, message = nil

断言响应是否成功,是否是指定的状态码。

assert_response :success代表200,assert_response :redirect代表300-399,assert_response :missing代表404,assert_response :error代表500-599。

assert_response :success

assert_response(200)

 

 

2.assert_redirected_to(options = {}, message=nil

验证跳转是否正确,是否跳转到指定的controller的action。

assert_redirected_to(:controller => "posts", :action => "index")

assert_redirected_to(:controller => "posts", :action => "show")

 

3.assert_template(expected = nil, message=nil

断言请求是否呈现了正确的页面。

 

 
assert_template "new"
是否呈现了new页面。 assert_template
:partial =
>
'_customer',
:locals =
> {
:customer =
>
@customer } 是否呈现了customer这个partial,并且加载了变量@customer
assert_template :partial => false 是否没有呈现任何的partial。

 

4.assert_tag  

断言响应的body中是否了指定条件的tag标签。

assert_tag :tag => "div", :attributes => { :id => "div-header" }验证响应的body中是否包含id=d"iv-header"的div标签。

 

5.assert_no_tag

和assert_tag相反,断言响应的body中是否不包含指定条件的tag。

 

6.assert_routing

断言route映射是否正确。

 

 
  1. def test_should_route_from_signout_to_destroy 
  2.     
  3.   assert_routing( {:path =>"signout", :method => :delete} , {:controller =>"sessions", :action =>"destroy", :method => :delete} )
  4.   
  5.  end 
  6.   

 

7.assert_select

断言view中的tag。

assert_select "form"

assert_select "title", "Welcome to my site!"

 

 

示例

我们还是拿我的项目做示例。

下面是针对sessionscontroller的new这个action的两个简单测试,测试这个action是否返回成功,并且加载了变量user。

 
  1. require 'test_helper' 
  2.  
  3. class SessionsControllerTest < ActionController::TestCase 
  4.   include FactoryGirl::Syntax::Methods  
  5.   def test_should_be_get_new 
  6.     get :new 
  7.     assert_response :success 
  8.     assert_not_nil assigns(@user
  9.   end 
  10.  
  11.   def test_should_be_get_new_status_code 
  12.     get :new 
  13.     assert_response(200)
  14.     assert_not_nil assigns(:user
  15.   end 
  16.  
  17.    
  18. end 

 

再来一个post的,这次我们测试signin这个过程,我们传入不存在的email和password,然后看看结果是不是我们想要的,因为不匹配,所以界面还是new template,同时输出flash信息。

 
  1. def test_should_be_render_new_template_after_email_do_not_match_password 
  2.   post :create,{
    :user=>{
    :email=>"",:password=>""}} 
  3.   assert_template :new 
  4. assert_equal("email and password do not match" , flash[:notice] )
  5. end 

 

再来添加一个用户验证成功的测试,看看成功之后是否符合我们的定义。验证flash信息,验证@controller的current_user是否可以获取到用户信息,@controller变量的signed_in?方法返回是否正确,正确登录之后应该返回true。

 
  1. def test_should_be_200_after_signin_success 
  2.     user = FactoryGirl.create(:user_valid
  3.  
  4.     post :create ,{
    :user=> {
    :email=>user.email,:password=>user.password}} 
  5.   
  6.     assert_equal( "sign in successfully", flash[:notice]) 
  7.     assert_not_nil @controller.current_user 
  8.     assert @controller.signed_in? 
  9.  
  10.  end 

 

再来添加一个针对userscontroller的测试,测试用户注册过程。

 
  1. require 'test_helper' 
  2.  
  3. class UsersControllerTest < ActionController::TestCase 
  4.   include FactoryGirl::Syntax::Methods 
  5.  
  6.   def test_should_be_200 
  7.     get :new 
  8.     assert_response(200) 
  9.   end 
  10.  
  11.   def test_should_be_stay_new_template_when_you_submit_invalid_signup_user 
  12.     user=FactoryGirl.build(:user_invalid_without_email
  13.     post :create, user 
  14.      
  15.     assert_template :new 
  16.     assert_equal "sign up failed!", flash[:notice
  17.   end 
  18.  
  19.   def test_should_be_successful_when_you_submit_valid_signup_user 
  20.  
  21.  
  22.     user =FactoryGirl.build(:user_valid
  23.     post :create, {
    :user=>{
    :email=>user.email,:nickname=>user.nickname, 
  24.                            :password=>user.password, 
  25.                            :password_confirmation => user.password_confirmation}} 
  26.     assert_equal  "sign up successfully!",flash[:notice
  27.     assert @controller.signed_in? 
  28.  
  29.   end 
  30.  
  31. end 

 

如果我们想要测试view中的tag是否和我们预期的相同,可以使用assert_select。

 
  1. def test_should_have_form_in_the_signin_page 
  2.     get :new 
  3.     assert_select "form" 
  4.     assert_select "h1""Sign up" 
  5.     assert_select "title""Sign up | Blog" 
  6.   end