From ab895dfea857ab44fd4d4e2ed3d305c9aa14ebb8 Mon Sep 17 00:00:00 2001 From: Joshua Clayton Date: Mon, 2 Aug 2010 11:20:51 -0400 Subject: [PATCH] Make flash.now more intuitive --- .../matchers/set_the_flash_matcher.rb | 7 +++--- .../controller/set_the_flash_matcher.rb | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) 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 a1e33933..13a36cae 100644 --- a/lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb +++ b/lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb @@ -66,10 +66,11 @@ module Shoulda # :nodoc: end def flash - flash_and_now = @controller.request.session["flash"] + return @flash if @flash + flash_and_now = @controller.request.session["flash"].dup if @controller.request.session["flash"] flash = @controller.send(:flash) - if @now + @flash = if @now flash.keys.each {|key| flash_and_now.delete(key) } flash_and_now else @@ -78,7 +79,7 @@ module Shoulda # :nodoc: end def expectation - expectation = "the flash to be set" + expectation = "the flash#{".now" if @now} to be set" expectation << " to #{@value.inspect}" unless @value.nil? expectation << ", but #{flash_description}" expectation diff --git a/test/matchers/controller/set_the_flash_matcher.rb b/test/matchers/controller/set_the_flash_matcher.rb index 4a20485e..f1b6c976 100644 --- a/test/matchers/controller/set_the_flash_matcher.rb +++ b/test/matchers/controller/set_the_flash_matcher.rb @@ -58,6 +58,30 @@ class SetTheFlashMatcherTest < ActionController::TestCase # :nodoc: end end + context "a controller that sets multiple flash messages" do + setup do + @controller = build_response { + flash.now[:notice] = 'value' + flash[:success] = 'great job' + } + end + + should "accept setting any flash.now message" do + assert_accepts set_the_flash.now, @controller + assert_accepts set_the_flash, @controller + end + + should "accept setting a matched flash.now message" do + assert_accepts set_the_flash.to(/value/).now, @controller + assert_accepts set_the_flash.to(/great/), @controller + end + + should "reject setting a different flash.now message" do + assert_rejects set_the_flash.to('other').now, @controller + assert_rejects set_the_flash.to('other'), @controller + end + end + context "a controller that doesn't set a flash message" do setup do @controller = build_response