How to Spec flash.now in One Easy Step

Jun 19th, 2008 — 2:51 PM

Was screwing around this morning trying to successfully spec that flash[:error] is set when the controller actually sets flash.now[:error]. A couple of different Google results had some different approaches none of which thrilled me. Then I tried completely mocking out the flash completely and got this lovely error:

Mock 'Flash now!' received unexpected message :sweep with (no args)

which gave me the brilliant idea to just stub :sweep on the flash. Lo and behold, it works and with a minimum of effort too.

it "should set flash[:error]" do
  flash.stub!(:sweep)
  post :create, params[:foo] # You'd have set this up for failure in the before block or whatever
  flash[:error].should_not be_nil # Spec'ing a specific error message is way brittle
end

Just thought I’d share. Hope you can benefit from it.

Filed under: rails, rspec

1 comment

  • blj said
    very useful.

Leave a Comment

Lucky Sneaks is designed and managed by Lucky Sneaks
All content © Lucky Sneaks 2008
Lucky Sneaks is Lucky Sneaks