我正在尝试使用 minitest 访问我的 Controller 中的实例变量。
例如:
microposts_controller.rb:
def destroy
p "*"*40
p @cats = 42
end
我如何使用 minitest 测试 @cats
的值?
我知道我可以从浏览器提交 delete
请求并检查我的服务器日志并找到:
"****************************************"
42
我读入了another answer我可以访问包含所有实例变量的 assigns
哈希,但它不起作用。我还尝试查看 controller
对象内部。如下图所示:
microposts_controller.rb:
test "@cats should exist in destroy method" do
delete micropost_path(@micropost)
p controller.instance_variables
p assigns[:cats]
end
输出:
[:@_action_has_layout, :@_routes, :@_request, :@_response, :@_lookup_context, :@_action_name, :@_response_body, :@marked_for_same_origin_verification, :@_config, :@_url_options]0:04
nil
我期待在 controller
对象中看到 @cats
实例变量。我还期待看到输出 42
。
我在这里错过了什么?
最佳答案
您可以使用view_assigns
:
# asserts that the controller has set @cats to true
assert_equal @controller.view_assigns['cats'], true
关于ruby - 使用 Minitest 访问 Controller 实例变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40963352/