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

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

 

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!"

 

 

示例

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

下面是针对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 

 




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

目录
相关文章
|
3月前
|
Java 测试技术 Maven
JAVA单元测试概念与实战
单元测试是软件开发中的一个测试方法,用于验证软件代码中最小的、独立的单元是否按照预期工作。在Java中,这通常指的是单个的方法或者一个类的个别功能。单元测试的目的是隔离代码的每个部分,并确保各个部分是正确的。
57 4
|
4月前
|
监控 数据可视化 Java
jvm性能调优实战 - 31从测试到上线_如何分析JVM运行状况及合理优化
jvm性能调优实战 - 31从测试到上线_如何分析JVM运行状况及合理优化
54 1
|
5天前
|
Java 测试技术 持续交付
自动化测试框架选型与实战:深入探索与应用
【5月更文挑战第8天】本文探讨了自动化测试框架的选型与实战应用,强调了其在软件质量保障中的重要性。选型原则包括考虑项目需求、技术栈、可扩展性和可维护性,以及社区支持和文档。介绍了Selenium、Appium、JUnit和Pytest等常用框架,并概述了实战应用的步骤,包括明确需求、搭建环境、编写测试用例、执行测试、分析结果、维护代码和持续集成。合理选型与实践能提升测试效率,保障项目成功。
|
5天前
|
算法 测试技术 开发者
测试驱动开发(TDD)实战:从理论到实践
【5月更文挑战第8天】TDD实战指南:先测试后开发,确保代码质量与可维护性。核心思想是编写测试用例→实现代码→验证→重构。优点包括提高代码质量、促进设计思考和增强可测试性。实战步骤包括编写独立、明确的测试用例,遵循最小可用原则编写代码,运行测试并分析失败原因,以及在验证通过后进行代码重构与优化。通过TDD,开发者能提升编程技能和项目成功率。
|
14天前
|
SQL 人工智能 自然语言处理
让老板成为数据分析师--ChatGpt链接本地数据源实战测试
本文探究ChatGpt等AI机器人能否帮助老板快速的做数据分析?用自然语言同老板进行沟通,满足老板的所有数据分析的诉求?
|
2月前
|
XML Java 测试技术
【Java优化实战】「微基准系列」带你脚踏实地的进行开发和使用JMH测试和提升应用程序和服务指南
【Java优化实战】「微基准系列」带你脚踏实地的进行开发和使用JMH测试和提升应用程序和服务指南
54 1
|
2月前
|
运维 数据库
Powershell实战:测试网络请求两个命令介绍
【2月更文挑战第11篇】 Test-Connection 命令将 Internet 控制消息协议 (ICMP) 回显请求数据包或 ping 发送给一台或多台远程计算机并返回回显响应回复。 我们可以使用该命令确定是否可通过 IP 网络ping通特定的计算机。
|
3月前
|
测试技术 API Python
Python自动化测试:unittest与pytest的实战技巧
Python自动化测试:unittest与pytest的实战技巧
|
4月前
|
测试技术 Shell Android开发
随机测试 Monkey Test
随机测试 Monkey Test
|
4月前
|
缓存
pytest 运行测试函数报错的解决办法 TypeError: calling <function xxx> returned None, not a test
pytest 运行测试函数报错的解决办法 TypeError: calling <function xxx> returned None, not a test
116 0

热门文章

最新文章