Permalink
Browse files

Added a block argument to should_return_from_session to replace the s…

…econd argument
  • Loading branch information...
1 parent c72d854 commit 3a3c576c5aa888780a76b34dd7cd46952f1ce68c @jferris jferris committed Feb 7, 2009
Showing with 23 additions and 9 deletions.
  1. +19 −9 lib/shoulda/controller/macros.rb
  2. +4 −0 test/functional/posts_controller_test.rb
@@ -153,19 +153,29 @@ def should_respond_with_content_type(content_type)
# set by the controller are available to the evaled string.
# Example:
#
- # should_return_from_session :user_id, '@user.id'
- # should_return_from_session :message, '"Free stuff"'
- def should_return_from_session(key, expected)
+ # should_return_from_session(:user_id) { '@user.id' }
+ # should_return_from_session(:message) { "Free stuff" }
+ def should_return_from_session(key, expected = nil, &block)
matcher = set_session(key)
+ if expected
+ warn "[DEPRECATION] should_return_from_session :key, 'val' is " <<
+ "deprecated. Use should_return_from_session(:key) { 'val' } " <<
+ "instead."
+ end
should matcher.description do
- instantiate_variables_from_assigns do
- expected_value = eval(expected,
- self.send(:binding),
- __FILE__,
- __LINE__)
+ if expected
+ instantiate_variables_from_assigns do
+ expected_value = eval(expected,
+ self.send(:binding),
+ __FILE__,
+ __LINE__)
+ matcher = matcher.to(expected_value)
+ end
+ else
+ expected_value = instance_eval(&block)
matcher = matcher.to(expected_value)
- assert_accepts matcher, @controller
end
+ assert_accepts matcher, @controller
end
end
@@ -71,6 +71,10 @@ def setup
should_respond_with_content_type /rss/
should_return_from_session :special, "'$2 off your next purchase'"
should_return_from_session :special_user_id, '@user.id'
+ should_return_from_session(:special_user_id) { @user.id }
+ should_fail do
+ should_return_from_session(:special_user_id) { 'value' }
+ end
should_assign_to :user, :posts
should_not_assign_to :foo, :bar
end

0 comments on commit 3a3c576

Please sign in to comment.