Permalink
Browse files

Converted should_set_the_flash_to/should_not_set_the_flash to use a m…

…atcher
  • Loading branch information...
1 parent f07f56e commit 048c4d1af1524b0a0f58d4d3ee24e5862157ceab @jferris jferris committed Feb 4, 2009
View
@@ -1,5 +1,4 @@
Unfinished:
-should_not_set_the_flash should_set_the_flash_to
should_render_with_layout should_render_without_layout
should_respond_with
should_respond_with_content_type
@@ -10,6 +9,8 @@ Finished:
should_assign_to
should_not_assign_to
should_filter_params
+should_not_set_the_flash
+should_set_the_flash_to
Won't implement:
should_be_restful
@@ -85,21 +85,19 @@ def should_be_restful(&blk) # :yields: resource
# should_set_the_flash_to /created/i
# should_set_the_flash_to nil
def should_set_the_flash_to(val)
- if val
- should "have #{val.inspect} in the flash" do
- assert_contains flash.values, val, ", Flash: #{flash.inspect}"
- end
- else
- should "not set the flash" do
- assert_equal({}, flash, "Flash was set to:\n#{flash.inspect}")
- end
+ matcher = set_the_flash.to(val)
+ should matcher.description do
+ assert_accepts matcher, @controller
end
end
# Macro that creates a test asserting that the flash is empty. Same as
# @should_set_the_flash_to nil@
def should_not_set_the_flash
- should_set_the_flash_to nil
+ matcher = set_the_flash
+ should "not #{matcher.description}" do
+ assert_rejects matcher, @controller
+ end
end
# Macro that creates a test asserting that filter_parameter_logging
@@ -1,5 +1,6 @@
require 'shoulda/controller/matchers/assign_to_matcher'
require 'shoulda/controller/matchers/filter_param_matcher'
+require 'shoulda/controller/matchers/set_the_flash_matcher'
module Shoulda # :nodoc:
module Controller # :nodoc:
@@ -0,0 +1,86 @@
+module Shoulda # :nodoc:
+ module Controller # :nodoc:
+ module Matchers
+
+ # 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) }
+ # it { should set_the_flash.to(nil) }
+ # it { should_not set_the_flash }
+ def set_the_flash
+ SetTheFlashMatcher.new
+ end
+
+ class SetTheFlashMatcher # :nodoc:
+
+ def to(value)
+ @value = value
+ self
+ end
+
+ def matches?(controller)
+ @controller = controller
+ sets_the_flash? && string_value_matches? && regexp_value_matches?
+ end
+
+ attr_reader :failure_message, :negative_failure_message
+
+ def description
+ description = "set the flash"
+ 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?
+ !flash.blank?
+ end
+
+ def string_value_matches?
+ return true unless String === @value
+ flash.values.any? {|value| value == @value }
+ end
+
+ def regexp_value_matches?
+ return true unless Regexp === @value
+ flash.values.any? {|value| value =~ @value }
+ end
+
+ def flash
+ @controller.response.session['flash']
+ end
+
+ def expectation
+ expectation = "the flash to be set"
+ 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
+
+ end
+
+ end
+ end
+end
@@ -0,0 +1,41 @@
+require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
+
+class SetTheFlashMatcherTest < Test::Unit::TestCase # :nodoc:
+
+ context "a controller that sets a flash message" do
+ setup do
+ @controller = build_response { flash[:notice] = 'value' }
+ end
+
+ should "accept setting any flash message" do
+ assert_accepts set_the_flash, @controller
+ end
+
+ should "accept setting the exact flash message" do
+ assert_accepts set_the_flash.to('value'), @controller
+ end
+
+ should "accept setting a matched flash message" do
+ assert_accepts set_the_flash.to(/value/), @controller
+ end
+
+ should "reject setting a different flash message" do
+ assert_rejects set_the_flash.to('other'), @controller
+ end
+
+ should "reject setting a different pattern" do
+ 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
+ end
+
+ should "reject setting any flash message" do
+ assert_rejects set_the_flash, @controller
+ end
+ end
+
+end

0 comments on commit 048c4d1

Please sign in to comment.