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条回答
霸刀☆藐视天下
2楼-- · 2019-03-09 20:36

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:

查看更多
登录 后发表回答