[英]In a Rails controller test, how to access a different controller action?
在功能测试中,我想在另一个控制器中调用一个动作。
You have to set the @controller
instance variable to the controller that should be used. 您必须将
@controller
实例变量设置为应该使用的控制器。
Example usage in a test helper method (of course you don't need to use it in a helper method - you can use it right in your test method): 测试帮助器方法中的示例用法(当然,您不需要在辅助方法中使用它 - 您可以在测试方法中使用它):
def login(user_name='user', password='asdfasdf')
# save the current controller
old_controller = @controller
# use the login controller
@controller = LoginController.new # <---
# perform the actual login
post :login, user_login: user_name, user_password: password
assert_redirected_to controller: 'welcome', action: 'index'
# check the users's values in the session
assert_not_nil session[:user]
assert_equal session[:user], User.find_by_login('user')
# restore the original controller
@controller = old_controller
end
Answered by Jonathan Weiss, in 2006 on ruby-forum: post() to other controller in functional test? 回答Jonathan Weiss,2006年在ruby-forum: post()到其他控制器进行功能测试?
It should be noted that, for the most part (probably >99.9% of the time), one should use integration tests (aka feature tests) for testing inter-controller behaviour. 应该注意的是,在大多数情况下(可能> 99.9%的时间),应该使用集成测试(也就是特征测试)来测试控制器间行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.