天天看点

Rails测试《七》实战功能测试functional test

之前我们介绍过,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) 

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

是否呈现了new页面。 assert_template :partial => '_customer', :locals => { :customer => @customer }

是否呈现了customer这个partial,并且加载了变量@customer

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映射是否正确。

def test_should_route_from_signout_to_destroy 

  assert_routing( {:path =>"signout", :method => :delete} , {:controller =>"sessions", :action =>"destroy", :method => :delete} )

 end 

7.assert_select

断言view中的tag。

assert_select "form"

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

示例

下面是针对sessionscontroller的new这个action的两个简单测试,测试这个action是否返回成功,并且加载了变量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 

    assert_response(200)

    assert_not_nil assigns(:user) 

end 

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

def test_should_be_render_new_template_after_email_do_not_match_password 

  post :create,{:user=>{:email=>"",:password=>""}} 

  assert_template :new 

assert_equal("email and password do not match" , flash[:notice] )

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

def test_should_be_200_after_signin_success 

    user = FactoryGirl.create(:user_valid) 

    post :create ,{:user=> {:email=>user.email,:password=>user.password}} 

    assert_equal( "sign in successfully", flash[:notice]) 

    assert_not_nil @controller.current_user 

    assert @controller.signed_in? 

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

class UsersControllerTest < ActionController::TestCase 

  include FactoryGirl::Syntax::Methods 

  def test_should_be_200 

    assert_response(200) 

  def test_should_be_stay_new_template_when_you_submit_invalid_signup_user 

    user=FactoryGirl.build(:user_invalid_without_email) 

    post :create, user 

    assert_template :new 

    assert_equal "sign up failed!", flash[:notice] 

  def test_should_be_successful_when_you_submit_valid_signup_user 

    user =FactoryGirl.build(:user_valid) 

    post :create, {:user=>{:email=>user.email,:nickname=>user.nickname, 

                           :password=>user.password, 

                           :password_confirmation => user.password_confirmation}} 

    assert_equal  "sign up successfully!",flash[:notice] 

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

def test_should_have_form_in_the_signin_page 

    assert_select "form" 

    assert_select "h1", "Sign up" 

    assert_select "title", "Sign up | Blog" 

本文转自 virusswb 51CTO博客,原文链接:http://blog.51cto.com/virusswb/1076787,如需转载请自行联系原作者