I get this feeling that speccing views in Rails may be pooh-poohed but with this being my first substantive Rails project I wanted to run the full gamut. As I'm using RSpec 2 I discovered that what I thought would be a readily available tag matching assertion is just gone; this is functionality that was available in RSpec 1 but removed due to its rigidness/difficulty to maintain and the fact that Webrat has have_selector for performing a similar task.
But I really wanted the simplistic and easily readable have_tag syntax so I looked around to see what else was out there. I stumbled across rspec2-rails-view-matchers which was exactly what I was looking for.
I ended up with the following code for testing that one of my show views has the required links for navigation (among other things):
#spec/views/army_lists/show.html.haml_spec.rb
require 'spec_helper'
describe "army_lists/show.html.haml" do
subject do
assign(:army_list,
stub_model(ArmyList,
:name => "Name",
:user => User.new do |u|
u.display_name = "John Smith"
u.id = 1
end,
:game_system => GameSystem.new do |gs|
gs.id = 1
gs.name = "Generic Game System"
end
)
)
render
rendered
end
it { should have_tag("a", :text => "John Smith") }
it { should have_tag("p", :text => /Name/m) }
it { should have_tag("a", :text => /Generic Game System/m) }
end