[rspec-users] Rails view spec expectations/matchers

Christopher J. Bottaro cjbottaro at gmail.com
Sat Oct 8 14:30:25 EDT 2011


>From looking at the RSpec Rails documentation 
(https://www.relishapp.com/rspec/rspec-rails/docs/view-specs/view-spec) it 
seems like rendered is just a string and you can't really do any 
assert_select type stuff out of the box.

After Googling around, it seems that the RSpec authors decided that if you 
want that functionality, you should just use Capybara or some such... is 
that correct?

If that is the case, can you please look at how I'm writing my view specs 
and let me know if there is a better/cleaner way?

Here goes...

*spec/spec_helper.rb*
RSpec.configure do |config|
  config.include RSpec::ViewHelper, :type => :view
end

*spec/support/view_helper.rb*
module RSpec::ViewHelper
  def page
    @page ||= Capybara::Node::Simple.new(rendered)
  end
end

*spec/views/comments/index.html.haml_spec.rb*
require "spec_helper"

describe "comments/index.html.haml" do

  it "should show a proper breadcrumb" do
    # Assign instance vars
    # Mock helper methods

    render

    page.should have_selector("div.breadcrumb")
    page.find("div.breadcrumb").tap do |node|
      node.find_link("Home").should be
      node.find_link("Blah1").should be
      node.find_link("Blah1").should be
      node.find_link("Comments").should be
    end
  end

end

Is there a better way?  Including Capybara just for the finders/matchers 
seems kinda heavy.  Also it would be nice if I could write stuff like node.should 
have_link("Home"), to be more consistent with RSpec matchers (though I guess 
I could write custom matchers to wrap all the Capybara stuff... has anyone 
already done this?).

Thanks for the help.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20111008/4f1593d9/attachment-0001.html>


More information about the rspec-users mailing list