diff --git a/lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb b/lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb index 52082b94e..9e60054d5 100644 --- a/lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb +++ b/lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb @@ -10,6 +10,7 @@ module ActionController # :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[:alert].to("Password doesn't match") } # it { should set_the_flash.to(/logged in/i).now } # it { should_not set_the_flash } def set_the_flash @@ -28,6 +29,11 @@ def now self end + def [](key) + @key = key + self + end + def matches?(controller) @controller = controller sets_the_flash? && string_value_matches? && regexp_value_matches? @@ -52,17 +58,25 @@ def negative_failure_message private def sets_the_flash? - !flash.blank? + flash_values.any? end def string_value_matches? return true unless String === @value - flash.to_hash.values.any? {|value| value == @value } + flash_values.any? {|value| value == @value } end def regexp_value_matches? return true unless Regexp === @value - flash.to_hash.values.any? {|value| value =~ @value } + flash_values.any? {|value| value =~ @value } + end + + def flash_values + if @key + [flash.to_hash[@key]] + else + flash.to_hash.values + end end def flash @@ -74,7 +88,7 @@ def flash end def expectation - expectation = "the flash#{".now" if @now} to be set" + expectation = "the flash#{".now" if @now}#{":[%s]" % @key if @key} to be set" expectation << " to #{@value.inspect}" unless @value.nil? expectation << ", but #{flash_description}" expectation diff --git a/spec/shoulda/action_controller/set_the_flash_matcher_spec.rb b/spec/shoulda/action_controller/set_the_flash_matcher_spec.rb index 2559eca7c..2e4247bf0 100644 --- a/spec/shoulda/action_controller/set_the_flash_matcher_spec.rb +++ b/spec/shoulda/action_controller/set_the_flash_matcher_spec.rb @@ -58,6 +58,44 @@ end end + context "a controller that sets a flash message of notice and alert" do + before do + @controller = build_response do + flash[:notice] = 'value' + flash[:alert] = 'other' + end + end + + it "should accept flash message of notice" do + @controller.should set_the_flash[:notice] + end + + it "should accept flash message of alert" do + @controller.should set_the_flash[:notice] + end + + it "should reject flash message of warning" do + @controller.should_not set_the_flash[:warning] + end + + it "should accept exact flash message of notice" do + @controller.should set_the_flash[:notice].to('value') + end + + it "should accept setting a matched flash message of notice" do + @controller.should set_the_flash[:notice].to(/value/) + end + + it "should reject setting a different flash message of notice" do + @controller.should_not set_the_flash[:notice].to('other') + end + + it "should reject setting a different pattern" do + @controller.should_not set_the_flash[:notice].to(/other/) + end + + end + context "a controller that sets multiple flash messages" do before do @controller = build_response {