使用Capybara检查多个页面上的页面响应 [英] Check page response on multiple pages with Capybara

查看:75
本文介绍了使用Capybara检查多个页面上的页面响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Capybara与Ruby on Rails结合使用来创建集成测试,该测试将填写所有字段并提交表单。然后,这会将新页面加载到浏览器。

 描述注册过程,:type => ; :request do 
它签到了
访问sign_up_path
fill_in‘user_first_name’,:with => Jhonny
t1 = Time.new
fill_in'user_email',:with => joe412@offerslot.com
fill_in‘user_last_name’,:with => Bravo
fill_in'user_zip_code',:with => 94102
fill_in'user_password',:with => password1234

click_button'用户按钮'
响应。应该be_success
response.body should_contain(下一页上的文本)
结束
结束


解决方案

如果使用水豚的访问,您将无法访问响应状态,因为水豚不提供该信息。如果要分析响应状态,则必须使用常规的HTTP请求动词(即 get post 等) ,但由于您的测试涉及多个页面,因此不适用于您描述的情况。



(使用 visit get 是常见的混淆点,请参见

所以基本上,答案是这样的:JoséValim撰写的 。是:不要在集成测试中测试响应状态。



但是您实际上不应该这样做。集成测试旨在测试用户实际看到的内容,即浏览器中显示的内容。您没有看到响应状态,看到的是实际页面及其中的任何内容,因此这实际上是您应该测试的内容。在上面的测试中,这就是您在 response.body should_contain(下一页上的文本)行所做的事情。足够确保响应成功。



如果您想测试应用程序中完全类似于API的方面,那么我建议您使用rspec请求规范为此(本质上是集成测试,但专注于单步请求/响应。)您可以使用 get 请求并使用水豚匹配器分析响应。


I'm using Capybara with Ruby on Rails to create an integration test that will fill in all of the fields and submit the form. This will then load a new page to the browser. Is it possible to get the response for this second page in the test?

describe "the signup process", :type => :request do
    it "signs me in" do
      visit sign_up_path
      fill_in 'user_first_name', :with => "Jhonny"
      t1 = Time.new
      fill_in 'user_email', :with => "joe412@offerslot.com"
      fill_in 'user_last_name', :with => "Bravo"
      fill_in 'user_zip_code', :with => "94102"
      fill_in 'user_password', :with => "password1234"

      click_button 'user-button'
      response.should be_success 
      response.body should_contain("Text on next page")
    end
end

解决方案

If you use capybara's visit you cannot access the response status, because capybara does not provide that info. If you want to analyze the response status you have to use the normal HTTP request verbs (i.e. get, post etc), but those won't work for the case you describe since your test involves multiple pages.

(The use of visit versus get is a common point of confusion. See this post by José Valim for details.)

So basically, the answer is: don't test the response status in your integration tests.

But you shouldn't really be doing this anyway. Integration tests are meant to test what the user actually sees, i.e. what comes up in the browser. You don't "see" a response status, what you see is the actual page and whatever is in it, so that's really what you should be testing. In your test above, that's what you're doing with the line response.body should_contain("Text on next page"). That's enough to ensure that the response was successful.

If you want to test aspects of your application which are exclusively API-like, then I'd recommend using rspec request specs for that (which are essentially integration tests but focused on one-step request/response.) You can use get requests and analyze the response using capybara matchers.

这篇关于使用Capybara检查多个页面上的页面响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆