Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added a block argument to should_return_from_session to replace the s…

…econd argument
  • Loading branch information...
commit 3a3c576c5aa888780a76b34dd7cd46952f1ce68c 1 parent c72d854
@jferris jferris authored
View
28 lib/shoulda/controller/macros.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
View
4 test/functional/posts_controller_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.