Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 940132787f72b658dd83c3f522af9a72ae6463b7 1 parent 30258b4
Joe Ferris jferris authored
8 lib/shoulda/action_controller/matchers/set_session_matcher.rb
@@ -26,7 +26,7 @@ def to(value)
26 26
27 27 def matches?(controller)
28 28 @controller = controller
29   - assigned_value? && assigned_correct_value?
  29 + (assigned_value? && assigned_correct_value?) || cleared_value?
30 30 end
31 31
32 32 def failure_message
@@ -39,7 +39,7 @@ def negative_failure_message
39 39
40 40 def description
41 41 description = "set session variable #{@key.inspect}"
42   - description << " to #{@value.inspect}" if @value
  42 + description << " to #{@value.inspect}" if defined?(@value)
43 43 description
44 44 end
45 45
@@ -49,6 +49,10 @@ def assigned_value?
49 49 !assigned_value.blank?
50 50 end
51 51
  52 + def cleared_value?
  53 + defined?(@value) && @value.nil? && assigned_value.nil?
  54 + end
  55 +
52 56 def assigned_correct_value?
53 57 return true if @value.nil?
54 58 assigned_value == @value
19 test/functional/posts_controller_test.rb
@@ -69,11 +69,20 @@ def setup
69 69 should_respond_with_content_type 'application/rss+xml'
70 70 should_respond_with_content_type :rss
71 71 should_respond_with_content_type /rss/
72   - should_return_from_session :special, "'$2 off your next purchase'"
  72 + context "deprecated" do # to avoid redefining a test
  73 + should_return_from_session :special, "'$2 off your next purchase'"
  74 + end
  75 + should_fail do
  76 + should_return_from_session :special, "'not special'"
  77 + end
  78 + should_set_session(:mischief) { nil }
  79 + should_return_from_session :malarky, "nil"
73 80 should_set_session :special, "'$2 off your next purchase'"
74 81 should_set_session :special_user_id, '@user.id'
75   - should_set_session(:special_user_id) { @user.id }
76   - should_fail do
  82 + context "with a block" do
  83 + should_set_session(:special_user_id) { @user.id }
  84 + end
  85 + should_fail do # to avoid redefining a test
77 86 should_set_session(:special_user_id) { 'value' }
78 87 end
79 88 should_assign_to :user, :posts
@@ -83,7 +92,9 @@ def setup
83 92 context "viewing a post on GET to #show" do
84 93 setup { get :show, :user_id => users(:first), :id => posts(:first) }
85 94 should_render_with_layout 'wide'
86   - should_render_with_layout :wide
  95 + context "with a symbol" do # to avoid redefining a test
  96 + should_render_with_layout :wide
  97 + end
87 98 should_assign_to :false_flag
88 99 end
89 100
4 test/matchers/controller/set_session_matcher_test.rb
@@ -22,6 +22,10 @@ class SetSessionMatcherTest < Test::Unit::TestCase # :nodoc:
22 22 should "reject assigning to another variable" do
23 23 assert_rejects set_session(:other), @controller
24 24 end
  25 +
  26 + should "accept assigning nil to another variable" do
  27 + assert_accepts set_session(:other).to(nil), @controller
  28 + end
25 29 end
26 30
27 31 end

0 comments on commit 9401327

Please sign in to comment.
Something went wrong with that request. Please try again.