mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
access assigns as a method or hash, with strings or symbols [#4431 state:resolved]
This commit is contained in:
parent
d4d352bf94
commit
4327ad51ab
2 changed files with 18 additions and 2 deletions
|
@ -3,13 +3,13 @@ require 'action_dispatch/middleware/flash'
|
|||
module ActionDispatch
|
||||
module TestProcess
|
||||
def assigns(key = nil)
|
||||
assigns = {}
|
||||
assigns = {}.with_indifferent_access
|
||||
@controller.instance_variable_names.each do |ivar|
|
||||
next if ActionController::Base.protected_instance_variables.include?(ivar)
|
||||
assigns[ivar[1..-1]] = @controller.instance_variable_get(ivar)
|
||||
end
|
||||
|
||||
key.nil? ? assigns : assigns[key.to_s]
|
||||
key.nil? ? assigns : assigns[key]
|
||||
end
|
||||
|
||||
def session
|
||||
|
|
|
@ -113,6 +113,11 @@ XML
|
|||
render :nothing => true
|
||||
end
|
||||
|
||||
def test_assigns
|
||||
@foo = "foo"
|
||||
render :nothing => true
|
||||
end
|
||||
|
||||
private
|
||||
def rescue_action(e)
|
||||
raise e
|
||||
|
@ -230,6 +235,17 @@ XML
|
|||
assert_equal "OK", @response.body
|
||||
end
|
||||
|
||||
def test_assigns
|
||||
process :test_assigns
|
||||
# assigns can be accessed using assigns(key)
|
||||
# or assigns[key], where key is a string or
|
||||
# a symbol
|
||||
assert_equal "foo", assigns(:foo)
|
||||
assert_equal "foo", assigns("foo")
|
||||
assert_equal "foo", assigns[:foo]
|
||||
assert_equal "foo", assigns["foo"]
|
||||
end
|
||||
|
||||
def test_assert_tag_tag
|
||||
process :test_html_output
|
||||
|
||||
|
|
Loading…
Reference in a new issue