Skip to content

Commit

Permalink
we need to eval what were sent
Browse files Browse the repository at this point in the history
  • Loading branch information
mjankowski committed Sep 12, 2008
1 parent c817375 commit 4c75faa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
15 changes: 10 additions & 5 deletions lib/shoulda/controller/macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,19 @@ def should_respond_with_content_type(content_type)
end
end

# Macro that creates a test asserting that the value returned from the session when accessed
# with the argument 'key' is equal to the value sent as the argument 'expected'
# Macro that creates a test asserting that a value returned from the session is correct.
# The given string is evaled to produce the resulting redirect path. All of the instance variables
# set by the controller are available to the evaled string.
# Example:
#
# should_return_from_session :user_id, @user.id
# should_return_from_session :user_id, nil
# 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
assert_equal expected, session[key], "Expected #{expected.inspect} but was #{session[key]}"
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]}"
end
end
end

Expand Down
4 changes: 3 additions & 1 deletion test/functional/posts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ def setup
context "viewing posts for a user with rss format" do
setup do
get :index, :user_id => users(:first), :format => 'rss'
@user = users(:first)
end
should_respond_with :success
should_respond_with_content_type 'application/rss+xml'
should_return_from_session :special, '$2 off your next purchase'
should_return_from_session :special, "'$2 off your next purchase'"
should_return_from_session :special_user_id, '@user.id'
should_assign_to :user, :posts
should_not_assign_to :foo, :bar
end
Expand Down
1 change: 1 addition & 0 deletions test/rails_root/app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def index
format.rss do
headers['Content-Type'] = 'application/rss+xml'
session[:special] = '$2 off your next purchase'
session[:special_user_id] = @user.id
head :ok
end
end
Expand Down

0 comments on commit 4c75faa

Please sign in to comment.