Rspec view testing with capybara and rails3
Solution 1
There is now an option to use Capybara matchers (without Webrat baggage) when testing controllers (and views too). I'm using it this way:
describe GlobalizeTranslationsController do
render_views
let(:page) { Capybara::Node::Simple.new(@response.body) }
describe "PUT :update" do
before do
put :update
end
it "displays a flash notice" do
page.should have_selector('p.notice')
end
end
end
Full code:
References:
- http://robots.thoughtbot.com/post/8087279685/use-capybara-on-any-html-fragment-or-page
- http://cakebaker.42dh.com/2010/09/19/cucumber-switching-from-webrat-to-capybara/
- http://trevorturk.com/2010/12/22/using-capybara-without-cucumber-in-rails-3
Solution 2
Capybara currently does not work with view specs (there are plans to make it work in the future). The simplest answer is to just add gem 'webrat'
to the Gemfile and you're basically set. You might not have have_button
but you'll have have_selector
, have_tag
and similar available.
Btw: as far as I know capybara and webrat can co-exist in one project.
Solution 3
Slightly simpler than Pawel's answer, but the gist is the same; the following works for me with rails 3.1.0, rspec 2.6.0, capybara 1.1.1:
page = Capybara::Node::Simple.new( rendered )
page.should have_content( "blah" )
Solution 4
You can't call capybara's methods on rendered
, that's just a string. You can use Capybara's string method though to wrap rendered
in a Capybara node. Then, you can call Capybara's methods on that node:
describe "some page" do
it "should render with lots of stuff" do
assign ..
render
Capybara.string(rendered).should have_button('Any button')
end
end
For more information, check out this post:
http://www.tamingthemindmonkey.com/2011/11/07/capybara-matchers-and-scoping-in-view-specs
Solution 5
At the bottom of this page, in the "Webrat and Capybara" section, it looks like Capybara is unsupported for rspec view specs
dahpgjgamgan
Updated on June 15, 2022Comments
-
dahpgjgamgan almost 2 years
I really like the way RSpec is able to separate controller and view tests but have some problems with getting capybara matchers to work in a view test. What i basically try to achieve is sth like this:
describe "some page" do it "should render with lots of stuff" do assign .. render rendered.should have_button ('Any button') #or any capybara matcher, really end end
I've seen some posts on the net showing how to configure capybara and rails3 to work smoothly with cucumber or rspec controller tests, but this is not really what I want - that is, testing the views at the lowest level possible.
Also if there's another way to do this (not requiring lots of custom code, couse I know i could write some matchers that extract given selectors from rendered using nokogiri or whatever tool suitable) that'd be great too - using capybara is not a requirement.
-
Jo Liss about 13 yearsCorrect. If you use Capybara's default driver (
:rack_test
), writing your view tests as "request tests" with Capybara should still give you reasonably good performance, by the way. It's how I essentially test my views. You just don't get to useassign
, so you have a little less control compared to "real" view tests. -
Paul Biggar over 12 yearsTHis is awesome - great answer!
-
Nicholas Pufal about 11 yearsI really don't recommend testing controllers like that. Using your example, that kind of test fits better on the view level.
-
Paweł Gościcki about 11 yearsFully agree! Controller specs should only test controller logic, not the views. If you want to test views create requests/features specs (using Capybara + Poltergeist or capybara-webkit).
-
Paulo Casaretto over 10 yearsHas anyone had any problems with Capybara matchers leaking into view tests?
-
rpearce about 10 yearsThis is a perfect case for view specs: relishapp.com/rspec/rspec-rails/v/2-0/docs/view-specs/view-spec
-
tgf over 8 yearsJust a note for anyone stumbling by these days: Capybara does support view specs now: github.com/jnicklas/capybara/blob/master/…