In a functional test, I want to call an action in another controller.
Asked
Active
Viewed 3,485 times
8
-
Possible duplicate of [How to call a different post to a different controller in Rails Functional Test](http://stackoverflow.com/questions/2815817/how-to-call-a-different-post-to-a-different-controller-in-rails-functional-test) – kolen Jun 29 '16 at 14:40
1 Answers
13
You have to set the @controller
instance variable to the controller that should be used.
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?
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.

user664833
- 18,397
- 19
- 91
- 140