ruby-on-rails-3viewrspec2capybaraspecs

How can you pass a block to have_selector?


I have the following view which I can't spec out properly in a view spec:

file: "products/index.html.haml"

#products
  = render @products

And this is my view spec:

require 'spec_helper'

describe "products/index.html.haml" do
  let(:products) {[mock_model(Product)]}

  before do
    stub_template "products/product.html.haml" => "" 
    render
  end

  it "should render the products" do
    rendered.should have_selector(#products) do
    rendered.should render_template products
  end
end

The problem here is that have_selector does not accept a block so there is no way to assert that the partial is rendered inside the #products div. Since Capybara matchers don't work within View specs you cannot use within either.

See also this issue: https://github.com/rspec/rspec-rails/issues/387


Solution

  • The correct answer is with webrat does take a block, and capybara does not.

    Here's the issue in which Jonas Nicklas, the creator of capybara explains why it doesn't and that he's not planning on adding it:

    https://github.com/jnicklas/capybara/issues/384

    There were some examples using Object#tap to pass blocks to capybara's #find that perhaps once worked, but don't seem to be working any longer.

    UPDATE: Confirmation from David that there is no way to do this presently with rspec/capybara:

    https://groups.google.com/forum/?fromgroups=#!topic/rspec/HBfznq4Yd0k