RSpec2和水豚 [英] RSpec2 and Capybara

查看:68
本文介绍了RSpec2和水豚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

水豚使我感到困惑。如果我将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_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的'访问':

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', 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屋!

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