Browse files

Fixed should_return_from_session with a nil value [#171 state:committed]

  • Loading branch information...
1 parent 30258b4 commit 940132787f72b658dd83c3f522af9a72ae6463b7 @jferris jferris committed Mar 5, 2009
View
8 lib/shoulda/action_controller/matchers/set_session_matcher.rb
@@ -26,7 +26,7 @@ def to(value)
def matches?(controller)
@controller = controller
- assigned_value? && assigned_correct_value?
+ (assigned_value? && assigned_correct_value?) || cleared_value?
end
def failure_message
@@ -39,7 +39,7 @@ def negative_failure_message
def description
description = "set session variable #{@key.inspect}"
- description << " to #{@value.inspect}" if @value
+ description << " to #{@value.inspect}" if defined?(@value)
description
end
@@ -49,6 +49,10 @@ def assigned_value?
!assigned_value.blank?
end
+ def cleared_value?
+ defined?(@value) && @value.nil? && assigned_value.nil?
+ end
+
def assigned_correct_value?
return true if @value.nil?
assigned_value == @value
View
19 test/functional/posts_controller_test.rb
@@ -69,11 +69,20 @@ def setup
should_respond_with_content_type 'application/rss+xml'
should_respond_with_content_type :rss
should_respond_with_content_type /rss/
- should_return_from_session :special, "'$2 off your next purchase'"
+ context "deprecated" do # to avoid redefining a test
+ should_return_from_session :special, "'$2 off your next purchase'"
+ end
+ should_fail do
+ should_return_from_session :special, "'not special'"
+ end
+ should_set_session(:mischief) { nil }
+ should_return_from_session :malarky, "nil"
should_set_session :special, "'$2 off your next purchase'"
should_set_session :special_user_id, '@user.id'
- should_set_session(:special_user_id) { @user.id }
- should_fail do
+ context "with a block" do
+ should_set_session(:special_user_id) { @user.id }
+ end
+ should_fail do # to avoid redefining a test
should_set_session(:special_user_id) { 'value' }
end
should_assign_to :user, :posts
@@ -83,7 +92,9 @@ def setup
context "viewing a post on GET to #show" do
setup { get :show, :user_id => users(:first), :id => posts(:first) }
should_render_with_layout 'wide'
- should_render_with_layout :wide
+ context "with a symbol" do # to avoid redefining a test
+ should_render_with_layout :wide
+ end
should_assign_to :false_flag
end
View
4 test/matchers/controller/set_session_matcher_test.rb
@@ -22,6 +22,10 @@ class SetSessionMatcherTest < Test::Unit::TestCase # :nodoc:
should "reject assigning to another variable" do
assert_rejects set_session(:other), @controller
end
+
+ should "accept assigning nil to another variable" do
+ assert_accepts set_session(:other).to(nil), @controller
+ end
end
end

0 comments on commit 9401327

Please sign in to comment.