Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

ruby on rails 3 - RSpec2 and Capybara

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"

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"

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So I just ran into similar, and this is I think what's going on:

It depends on code you didn't include here of how you visit the page. I'm writing an rspec request spec.

If I retrieve the page with rspec's own:

get '/some/path'

then response.body.should have_selector works as you say, but page.should does not.

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.

This does get confusing, all the mixing and matching of different libraries involved in rails testing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...