Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 939
0x4a6f4672 Avatar asked Aug 31 '11 17:08

0x4a6f4672


People also ask

What is RSpec capybara?

Capybara is a web-based test automation software that simulates scenarios for user stories and automates web application testing for behavior-driven software development. It is written in the Ruby programming language.

What is the difference between RSpec and cucumber?

The main difference between RSpec and Cucumber are the business readability factor. Cucumber's main draw is that the specification (features) are separate from the test code, so your product owners can provide or review the specification without having to dig through code.

What is RSpec used for?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.

Is RSpec TDD or BDD?

RSpec is a Behavior-Driven Development tool for Ruby programmers. BDD is an approach to software development that combines Test-Driven Development, Domain Driven Design and Acceptance Test-Driven Planning. RSpec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD.


1 Answers

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.

like image 66
jrochkind Avatar answered Sep 25 '22 02:09

jrochkind