Rspec view testing with capybara and rails3

2019-03-09 19:56发布

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.

7条回答
Animai°情兽
2楼-- · 2019-03-09 20:17

Updating this old question as things have changed since most of the other answers were added:

Capybara now does support view specs (out of the box) and this is documented on Capybara's master branch.

To quote the docs:

Finally, Capybara matchers are supported in view specs:

RSpec.describe "todos/show.html.erb", type: :view do
  it "displays the todo title" do
    assign :todo, Todo.new(title: "Buy milk")

    render

    expect(rendered).to have_css("header h1", text: "Buy milk")
  end
end

Support for these without additional let(:page) style code appears to have been added in an earlier version. (It's working for me in capybara 2.4.4 and 2.2).
Note also that only a limited subset of matchers are supported; however you can gain more functionality by using Capybara.string; ex:

expect(Capybara.string(rendered).first('td')).to have_no_content 'Tom Riddle'
查看更多
beautiful°
3楼-- · 2019-03-09 20:20

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

查看更多
爷的心禁止访问
4楼-- · 2019-03-09 20:24

You can also use capybara syntax

describe "some page" do 
  it 'should render hello with name' do
    assign(:user, double("User", first_name: "John"))
    render
    expect(rendered).to have_content("Hello John")
  end
end
查看更多
女痞
5楼-- · 2019-03-09 20:27

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" )
查看更多
地球回转人心会变
6楼-- · 2019-03-09 20:29

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.

查看更多
男人必须洒脱
7楼-- · 2019-03-09 20:34

At the bottom of this page, in the "Webrat and Capybara" section, it looks like Capybara is unsupported for rspec view specs

http://relishapp.com/rspec/rspec-rails

查看更多
登录 后发表回答