diff --git a/lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb b/lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb index ca53fcc8..a1e33933 100644 --- a/lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb +++ b/lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb @@ -10,6 +10,7 @@ module Shoulda # :nodoc: # it { should set_the_flash } # it { should set_the_flash.to("Thank you for placing this order.") } # it { should set_the_flash.to(/created/i) } + # it { should set_the_flash.to(/logged in/i).now } # it { should_not set_the_flash } def set_the_flash SetTheFlashMatcher.new @@ -22,6 +23,11 @@ module Shoulda # :nodoc: self end + def now + @now = true + self + end + def matches?(controller) @controller = controller sets_the_flash? && string_value_matches? && regexp_value_matches? @@ -60,7 +66,15 @@ module Shoulda # :nodoc: end def flash - @controller.send(:flash) + flash_and_now = @controller.request.session["flash"] + flash = @controller.send(:flash) + + if @now + flash.keys.each {|key| flash_and_now.delete(key) } + flash_and_now + else + flash + end end def expectation diff --git a/test/matchers/controller/set_the_flash_matcher.rb b/test/matchers/controller/set_the_flash_matcher.rb index 320a722c..4a20485e 100644 --- a/test/matchers/controller/set_the_flash_matcher.rb +++ b/test/matchers/controller/set_the_flash_matcher.rb @@ -28,6 +28,36 @@ class SetTheFlashMatcherTest < ActionController::TestCase # :nodoc: end end + context "a controller that sets a flash.now message" do + setup do + @controller = build_response { flash.now[:notice] = 'value' } + end + + should "reject setting any flash message" do + assert_rejects set_the_flash, @controller + end + + should "accept setting any flash.now message" do + assert_accepts set_the_flash.now, @controller + end + + should "accept setting the exact flash.now message" do + assert_accepts set_the_flash.to('value').now, @controller + end + + should "accept setting a matched flash.now message" do + assert_accepts set_the_flash.to(/value/).now, @controller + end + + should "reject setting a different flash.now message" do + assert_rejects set_the_flash.to('other').now, @controller + end + + should "reject setting a different flash.now pattern" do + assert_rejects set_the_flash.to(/other/).now, @controller + end + end + context "a controller that doesn't set a flash message" do setup do @controller = build_response