RSpec2 和水豚 [英] RSpec2 and Capybara
问题描述
水豚让我很困惑.如果我将 Capybara 与 Ruby on Rails 3 和 RSpec 2 结合使用,则在 RSpec 请求测试中,以下匹配器有效:
Capybara is confusing me. If I use Capybara in combination with Ruby on Rails 3 and RSpec 2, then in RSpec request tests, the following matcher works:
response.body.should have_selector "div.some_class"
响应对象具有 ActionDispatch::TestResponse
类.但是下面这行 应该正式工作,却不起作用:
The response object has the class ActionDispatch::TestResponse
. But the following line, which should work officially, does not work:
page.should have_selector "div.some_class"
页面对象具有 Capybara::Session
类.在哪些情况下必须使用 response.body
对象,何时必须使用 page
对象?
The page object has the class Capybara::Session
. In which cases do you have to use the response.body
object and when do you have to use a page
object ?
推荐答案
所以我刚刚遇到了类似的情况,我认为这是怎么回事:
So I just ran into similar, and this is I think what's going on:
这取决于您在此处未包含的有关您如何访问页面的代码.我正在编写 rspec 请求规范.
It depends on code you didn't include here of how you visit the page. I'm writing an rspec request spec.
如果我用 rspec 自己的检索页面:
If I retrieve the page with rspec's own:
get '/some/path'
然后 response.body.should have_selector 像你说的那样工作,但 page.should 没有.
then response.body.should have_selector works as you say, but page.should does not.
要使 Capybara 的页面"工作(并使 Capybara 交互如 click_button 或 fill_in 工作),而不是使用 rspec 的 'get' 检索,您需要使用 Capybara 的 'visit' 检索:
To make Capybara 'page' work (and to make Capybara interactions like click_button or fill_in work), instead of retrieving with rspec's 'get', you need to retrieve with Capybara's 'visit':
visit '/some/path'
page.should have_selector("works")
'page' 是一种水豚方法,只有在使用水豚方法 'visit' 时才会设置.
'page', a capybara method, only gets set when using 'visit', a capybara method.
这确实令人困惑,rails 测试中涉及的不同库的所有混合和匹配.
This does get confusing, all the mixing and matching of different libraries involved in rails testing.
这篇关于RSpec2 和水豚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!