Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Enable set_the_flash matcher to test for flash.now

  • Loading branch information...
commit 96a80acb59d6eafb52b7f1f9aa46c2abef165b0e 1 parent f814549
@joshuaclayton joshuaclayton authored
View
16 lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb
@@ -10,6 +10,7 @@ module Matchers
# 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(/logged in/i).now }
# it { should_not set_the_flash }
def set_the_flash
SetTheFlashMatcher.new
@@ -22,6 +23,11 @@ def to(value)
self
end
+ def now
+ @now = true
+ self
+ end
+
def matches?(controller)
@controller = controller
sets_the_flash? && string_value_matches? && regexp_value_matches?
@@ -60,7 +66,15 @@ def regexp_value_matches?
end
def flash
- @controller.send(:flash)
+ flash_and_now = @controller.request.session["flash"]
+ flash = @controller.send(:flash)
+
+ if @now
+ flash.keys.each {|key| flash_and_now.delete(key) }
+ flash_and_now
+ else
+ flash
+ end
end
def expectation
View
30 test/matchers/controller/set_the_flash_matcher.rb
@@ -28,6 +28,36 @@ class SetTheFlashMatcherTest < ActionController::TestCase # :nodoc:
end
end
+ context "a controller that sets a flash.now message" do
+ setup do
+ @controller = build_response { flash.now[:notice] = 'value' }
+ end
+
+ should "reject setting any flash message" do
+ assert_rejects set_the_flash, @controller
+ end
+
+ should "accept setting any flash.now message" do
+ assert_accepts set_the_flash.now, @controller
+ end
+
+ should "accept setting the exact flash.now message" do
+ assert_accepts set_the_flash.to('value').now, @controller
+ end
+
+ should "accept setting a matched flash.now message" do
+ assert_accepts set_the_flash.to(/value/).now, @controller
+ end
+
+ should "reject setting a different flash.now message" do
+ assert_rejects set_the_flash.to('other').now, @controller
+ end
+
+ should "reject setting a different flash.now pattern" do
+ assert_rejects set_the_flash.to(/other/).now, @controller
+ end
+ end
+
context "a controller that doesn't set a flash message" do
setup do
@controller = build_response
Please sign in to comment.
Something went wrong with that request. Please try again.