温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Rails测试《九》集成测试integration test

发布时间:2020-06-27 01:57:07 阅读:1589 作者:virusswb 栏目:软件技术
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

开场白

今天我们来熟悉一下rails的集成测试integration test。

简介

集成测试主要是测试多个controller之间的交互,以及测试应用中比较重要的工作流程,验证这些工作流程是否符合预期的设想。

不像单元测试和功能测试,是自动添加的。集成测试是需要我们手动添加的,rails提供了一个命令

rails generate integration_test

通过命令就可以在test/integration文件夹创建集成测试。

$ rails generate integration_test user_flows       exists  test/integration/       create  test/integration/user_flows_test.rb 

我们先来了解一些集成测试中常用的帮助方法。

https? 

如果session正在模拟https请求,就返回true。

https! 

允许你模拟https请求。

host! 

允许你在下一次的请求中设置host name。

redirect? 

如果上一次请求是一个跳转,就返回true。

follow_redirect! 

紧跟着一个跳转的响应

request_via_redirect(http_method, path, [parameters][headers]

向指定的path发送http请求,可选parameters,可选headers,然后跟着一个跳转。

post_via_redirect(path, [parameters][headers]

向指定的path发送post请求,可选parameters,可选headers,然后跟着一个跳转。

get_via_redirect(path, [parameters][headers]

向指定的path发送get请求,可选parameters,可选headers,然后跟着一个跳转。

put_via_redirect(path, [parameters][headers]

向指定的path发送put请求,可选parameters,可选headers,然后跟着一个跳转。

delete_via_redirect(path, [parameters][headers]

向指定的path发送delete请求,可选parameters,可选headers,然后跟着一个跳转。

open_session 

开启一个新的session

示例

让我们在创建好的集成测试文件test/integration/user_flows_test.rb中添加一些代码。

require 'test_helper'  class UserFlowsTest < ActionDispatch::IntegrationTest   include FactoryGirl::Syntax::Methods    def test_admin_login_and_browse_posts     user = FactoryGirl.create(:user_valid)      get "/signin"     assert_response(200)      post_via_redirect("sessions", {:user=>{:email=> user.email, :password => user.password}})     assert_equal "/", path     assert_equal "sign in successfully", flash[:notice]      get "admin/posts"     assert_response(200)     assert assigns(:posts)      end  end 

上面的代码中,我们先是在users表中添加了一条记录。然后访问signin,然后断言是否返回200.

然后向sessions提交刚才添加的用户邮箱和密码,sessionscontroller是负责验证用户信息的controller。然后断言是否跳转到了根目录,是否出现了正确的flash提示信息。

最后访问admin/posts,断言是否返回200,并且返回posts变量。

上面的测试涉及了多个controller,测试覆盖从数据库到controller的调度分配。

我们可以同时模拟多个session,并且用extend扩展这些session,创建一些强大的测试用的DSL(Domain-Specific Language 领域描述语言)。

我们把上面的测试改成下面的样子。

require 'test_helper'  class UserFlowsTest < ActionDispatch::IntegrationTest   include FactoryGirl::Syntax::Methods    def test_admin_login_and_browse_posts     user = FactoryGirl.create(:user_valid)     guest = FactoryGirl.create(:user_valid_too)       user_session = signin(user)     guest_session = signin(guest)      assert_equal("sign in successfully", user_session.flash[:notice])     assert_equal("sign in successfully", guest_session.flash[:notice])      user_session.browse_site     guest_session.browse_site      end    private    module CustomDSL     def browse_site       get "admin/posts"       assert_response(200)       assert assigns(:posts)     end   end    def signin(user)     open_session do |sess|       sess.extend(CustomDSL)       sess.post_via_redirect("sessions", {:user => {:email => user.email, :password => user.password}})            end   end  end 

什么是DSL(领域描述语言)呢?

我理解就是业务描述语言。我们的应用一般是面向一个行业,或者说面向一个领域的,业务的语言就是领域描述语言。

如果能用这个领域的业务语言描述测试过程,那么这个测试就更加贴近业务,具有了很强的沟通能力。也就是说这个测试可以拿来和业务进行沟通,看看是不是他们想要的业务过程。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×