In a Rails controller test, how do I access another controller action?

In a functional test, I want to trigger an action in another controller.

+6
source share
1 answer

You must set the @controller instance @controller for the controller to be used.

An example of using a test helper in a method (of course, you do not need to use it in a helper method - you can use it directly 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 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 answered in 2006 at ruby-forum: post () for another controller in a functional test?

It should be noted that, for the most part (probably> 99.9% of the time), integration tests (e.g., function tests) should be used to test the behavior between controllers.

+9
source

All Articles