之前我們介紹過,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,如需轉載請自行聯系原作者