简体   繁体   English

在Rails控制器测试中,如何访问不同的控制器操作?

[英]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.

相关问题 Rails 4:如何测试控制器动作 - Rails 4: How to test Controller Action 如何在 Rails 中访问 URL 在 Controller 中显示操作 - How to access URL in Rails Show action in Controller 如何将不同的控制器名称与其他控制器动作组合? 滑轨 - how to combine different controller name with other controller action? Rails Rails-在控制器中,如何从其他控制器渲染动作? - Rails - in controller, how to render an action from a different controller? Rails在不同控制器中的渲染动作 - Rails Rendering Action in Different Controller Rails 3.1 Rspec Rails 2-如何测试此控制器操作? - Rails 3.1 Rspec Rails 2 - How can I test this controller action? 如何在Rails 4.2中的动作中呈现不同的控制器/动作? - How to render a different controller / action from within an action in Rails 4.2? 如何在Rails控制器动作的RSpec测试中指定查询字符串? - How to specifiy the query string in an RSpec test of a Rails controller action? 如何在Rails Functional Test中为不同的控制器调用不同的帖子 - How to call a different post to a different controller in Rails Functional Test Rails:如何防止未经授权访问控制器更新操作 - Rails: How to prevent unauthorized access of controller update action
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM