2010-12-15 17:34:19 -05:00
|
|
|
module Shoulda # :nodoc:
|
|
|
|
module Matchers
|
|
|
|
module ActionController # :nodoc:
|
|
|
|
|
|
|
|
# Ensures that the flash contains the given value. Can be a String, a
|
|
|
|
# Regexp, or nil (indicating that the flash should not be set).
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# 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) }
|
2011-12-16 03:26:29 -05:00
|
|
|
# it { should set_the_flash[:alert].to("Password doesn't match") }
|
2010-12-15 17:34:19 -05:00
|
|
|
# it { should set_the_flash.to(/logged in/i).now }
|
|
|
|
# it { should_not set_the_flash }
|
|
|
|
def set_the_flash
|
|
|
|
SetTheFlashMatcher.new
|
|
|
|
end
|
|
|
|
|
|
|
|
class SetTheFlashMatcher # :nodoc:
|
2012-05-07 11:17:33 -04:00
|
|
|
def initialize
|
|
|
|
@options = {}
|
|
|
|
end
|
|
|
|
|
2012-03-30 10:51:00 -04:00
|
|
|
attr_reader :failure_message, :negative_failure_message
|
2010-12-15 17:34:19 -05:00
|
|
|
|
|
|
|
def to(value)
|
|
|
|
@value = value
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def now
|
2012-05-07 11:17:33 -04:00
|
|
|
@options[:now] = true
|
2010-12-15 17:34:19 -05:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2011-12-16 03:26:29 -05:00
|
|
|
def [](key)
|
2012-05-07 11:17:33 -04:00
|
|
|
@options[:key] = key
|
2011-12-16 03:26:29 -05:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2010-12-15 17:34:19 -05:00
|
|
|
def matches?(controller)
|
|
|
|
@controller = controller
|
|
|
|
sets_the_flash? && string_value_matches? && regexp_value_matches?
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
2011-12-16 04:00:02 -05:00
|
|
|
description = "set the #{expected_flash_invocation}"
|
2010-12-15 17:34:19 -05:00
|
|
|
description << " to #{@value.inspect}" unless @value.nil?
|
|
|
|
description
|
|
|
|
end
|
|
|
|
|
|
|
|
def failure_message
|
|
|
|
"Expected #{expectation}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def negative_failure_message
|
|
|
|
"Did not expect #{expectation}"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def sets_the_flash?
|
2011-12-16 03:26:29 -05:00
|
|
|
flash_values.any?
|
2010-12-15 17:34:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def string_value_matches?
|
2012-03-30 10:51:00 -04:00
|
|
|
if @value.is_a?(String)
|
|
|
|
flash_values.any? {|value| value == @value }
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
2010-12-15 17:34:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def regexp_value_matches?
|
2012-03-30 10:51:00 -04:00
|
|
|
if @value.is_a?(Regexp)
|
|
|
|
flash_values.any? {|value| value =~ @value }
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
2011-12-16 03:26:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def flash_values
|
2012-05-07 11:17:33 -04:00
|
|
|
if @options.key?(:key)
|
|
|
|
[flash.to_hash[@options[:key]]]
|
2011-12-16 03:26:29 -05:00
|
|
|
else
|
|
|
|
flash.to_hash.values
|
|
|
|
end
|
2010-12-15 17:34:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def flash
|
2012-03-30 10:51:00 -04:00
|
|
|
if @flash
|
|
|
|
@flash
|
|
|
|
else
|
|
|
|
@flash = @controller.flash.dup
|
|
|
|
@flash.instance_variable_set(:@used, @controller.flash.instance_variable_get(:@used).dup)
|
2012-05-07 11:17:33 -04:00
|
|
|
if ! @options[:now]
|
2012-03-30 10:51:00 -04:00
|
|
|
@flash.sweep
|
|
|
|
end
|
|
|
|
@flash
|
|
|
|
end
|
2010-12-15 17:34:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def expectation
|
2011-12-16 04:00:02 -05:00
|
|
|
expectation = "the #{expected_flash_invocation} to be set"
|
2010-12-15 17:34:19 -05:00
|
|
|
expectation << " to #{@value.inspect}" unless @value.nil?
|
|
|
|
expectation << ", but #{flash_description}"
|
|
|
|
expectation
|
|
|
|
end
|
|
|
|
|
|
|
|
def flash_description
|
|
|
|
if flash.blank?
|
|
|
|
"no flash was set"
|
|
|
|
else
|
|
|
|
"was #{flash.inspect}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-16 04:00:02 -05:00
|
|
|
def expected_flash_invocation
|
2012-05-07 11:17:33 -04:00
|
|
|
"flash#{pretty_now}#{pretty_key}"
|
|
|
|
end
|
2011-12-16 04:00:02 -05:00
|
|
|
|
2012-05-07 11:17:33 -04:00
|
|
|
def pretty_now
|
|
|
|
if @options[:now]
|
|
|
|
".now"
|
|
|
|
else
|
|
|
|
""
|
2012-03-09 12:17:59 -05:00
|
|
|
end
|
2012-05-07 11:17:33 -04:00
|
|
|
end
|
2010-12-15 17:34:19 -05:00
|
|
|
|
2012-05-07 11:17:33 -04:00
|
|
|
def pretty_key
|
|
|
|
if @options[:key]
|
|
|
|
"[:#{@key}]"
|
|
|
|
else
|
|
|
|
""
|
2012-03-09 12:17:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-12-15 17:34:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|