在功能测试中,我想在另一个 Controller 中调用一个 Action 。
最佳答案
您必须将@controller
实例变量 设置为应该使用的 Controller 。
测试辅助方法中的示例用法(当然你不需要在辅助方法中使用它——你可以直接在你的测试方法中使用它):
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
由 Jonathan Weiss 于 2006 年在 ruby-forum 上回答:post() to other controller in functional test?
应该注意的是,在大多数情况下(可能 >99.9% 的时间),应该使用集成测试(也称为功能测试)来测试 Controller 间的行为。
关于ruby-on-rails - 在 Rails Controller 测试中,如何访问不同的 Controller 操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22725883/