RSpec Rails Flash Matcher

UPDATE (08 Jan 2010): Okay, I have no clue how I missed it, but Shoulda already has a flash matcher.  You can find more details in their GitHub repo.

I just started using Shoulda's very nice RSpec custom matchers. They do a great job of trimming the fat off your specs. One thing I found missing though was a simple matcher to handle the Rails flash. A quick googling didn't turn up any simple matchers for this, so I put one together and pushed it to github -- rspec-flash-matcher.

It's a pretty straightforward matcher, and lets you reduce

it "should set the error flash" do
  flash[:error].should_not be_nil
end

to

it { should set_flash(:error) }

If you like to test actual flash output, you can use

it { should set_flash(:error).to("OMG u broked it") }

I plan to add regex support soon. Also, I have a suspicion that this won't work under Ruby 1.9 because of changes in how send handles protected methods.

blog comments powered by Disqus