Skip to content

Commit

Permalink
Add SetTheFlashMatcher#[] to specify flash type
Browse files Browse the repository at this point in the history
  • Loading branch information
Fujimura Daisuke authored and Gabe Berke-Williams committed Mar 9, 2012
1 parent 85c37c4 commit ef866e2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb
Expand Up @@ -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
Expand All @@ -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?
Expand All @@ -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
Expand All @@ -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
Expand Down
38 changes: 38 additions & 0 deletions spec/shoulda/action_controller/set_the_flash_matcher_spec.rb
Expand Up @@ -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 {
Expand Down

0 comments on commit ef866e2

Please sign in to comment.