Permalink
Browse files

Converted should_return_from_session to use a matcher

  • Loading branch information...
1 parent 56ebdc8 commit 0a2f0126df4e45d32c816e6595ec666f0264a5f8 @jferris jferris committed Feb 5, 2009
View
@@ -1,7 +1,6 @@
Unfinished:
should_respond_with
should_respond_with_content_type
-should_return_from_session
should_route
Finished:
@@ -12,6 +11,7 @@ should_not_set_the_flash
should_set_the_flash_to
should_render_with_layout
should_render_without_layout
+should_return_from_session
Won't implement:
should_be_restful
@@ -203,10 +203,15 @@ def should_respond_with_content_type(content_type)
# should_return_from_session :user_id, '@user.id'
# should_return_from_session :message, '"Free stuff"'
def should_return_from_session(key, expected)
- should "return the correct value from the session for key #{key}" do
+ matcher = set_session(key)
+ should matcher.description do
instantiate_variables_from_assigns do
- expected_value = eval(expected, self.send(:binding), __FILE__, __LINE__)
- assert_equal expected_value, session[key], "Expected #{expected_value.inspect} but was #{session[key]}"
+ expected_value = eval(expected,
+ self.send(:binding),
+ __FILE__,
+ __LINE__)
+ matcher = matcher.to(expected_value)
+ assert_accepts matcher, @controller
end
end
end
@@ -3,6 +3,7 @@
require 'shoulda/controller/matchers/set_the_flash_matcher'
require 'shoulda/controller/matchers/render_with_layout_matcher'
require 'shoulda/controller/matchers/respond_with_matcher'
+require 'shoulda/controller/matchers/set_session_matcher'
module Shoulda # :nodoc:
module Controller # :nodoc:
@@ -0,0 +1,83 @@
+module Shoulda # :nodoc:
+ module Controller # :nodoc:
+ module Matchers
+
+ # Ensures that a session key was set to the expected value.
+ #
+ # Example:
+ #
+ # it { should set_session(:message) }
+ # it { should set_session(:user_id).to(@user.id) }
+ # it { should_not set_session(:user_id) }
+ def set_session(key)
+ SetSessionMatcher.new(key)
+ end
+
+ class SetSessionMatcher # :nodoc:
+
+ def initialize(key)
+ @key = key.to_s
+ end
+
+ def to(value)
+ @value = value
+ self
+ end
+
+ def matches?(controller)
+ @controller = controller
+ assigned_value? && assigned_correct_value?
+ end
+
+ def failure_message
+ "Expected #{expectation}, but #{result}"
+ end
+
+ def negative_failure_message
+ "Didn't expect #{expectation}, but #{result}"
+ end
+
+ def description
+ description = "set session variable #{@key.inspect}"
+ description << " to #{@value.inspect}" if @value
+ description
+ end
+
+ private
+
+ def assigned_value?
+ !assigned_value.blank?
+ end
+
+ def assigned_correct_value?
+ return true if @value.nil?
+ assigned_value == @value
+ end
+
+ def assigned_value
+ session[@key]
+ end
+
+ def session
+ @controller.response.session.data
+ end
+
+ def expectation
+ expectation = "session variable #{@key} to be set"
+ expectation << " to #{@value.inspect}" if @value
+ expectation
+ end
+
+ def result
+ if session.empty?
+ "no session variables were set"
+ else
+ "the session was #{session.inspect}"
+ end
+ end
+
+ end
+
+ end
+ end
+end
@@ -0,0 +1,27 @@
+require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
+
+class SetSessionMatcherTest < Test::Unit::TestCase # :nodoc:
+
+ context "a controller that sets a session variable" do
+ setup do
+ @controller = build_response { session[:var] = 'value' }
+ end
+
+ should "accept assigning to that variable" do
+ assert_accepts set_session(:var), @controller
+ end
+
+ should "accept assigning the correct value to that variable" do
+ assert_accepts set_session(:var).to('value'), @controller
+ end
+
+ should "reject assigning another value to that variable" do
+ assert_rejects set_session(:var).to('other'), @controller
+ end
+
+ should "reject assigning to another variable" do
+ assert_rejects set_session(:other), @controller
+ end
+ end
+
+end

0 comments on commit 0a2f012

Please sign in to comment.