# Functional testing -- using post() w/ a different controller

**URL:** <https://www.ruby-forum.com/t/functional-testing-using-post-w-a-different-controller/48272>\
**Category:** Rails\
**Created:** [November 18, 2005, 12:06pm UTC](https://www.ruby-forum.com/t/functional-testing-using-post-w-a-different-controller/48272 "2005-11-18T12:06:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [November 18, 2005, 12:06pm UTC](https://www.ruby-forum.com/t/functional-testing-using-post-w-a-different-controller/48272/1 "2005-11-18T12:06:30Z")

</div>

Hallo – I have a function in test\_helper.rb that looks not a little  
like this:

def  
login(username=“gowondapiddlehead”,unhashed\_password=“gowonda\_piddlehead”)  
post(:login,:system\_user=\>{:username=\>username,:unhashed\_password=\>unhashed\_password})  
assert\_redirected\_to(:controller=\>‘home’)  
assert\_not\_nil(session[:system\_user\_id])  
system\_user=SystemUser.find(session[:system\_user\_id])  
assert\_equal(username,system\_user.username,“Login name should match  
session name.”)  
end

When I run the test, however, I get the following error:

```
1) Error:

```

test\_index(HomeControllerTest):  
NoMethodError: undefined method `login' for Test::Unit::TestCase:Class test/functional/home_controller_test.rb:18:in`test\_index’

I guess this is because I need to specify the controller being called as  
well as the action in the post() function.  
Does anyone know how to do this?  
Cheers,  
doug.

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [December 19, 2005, 8:22am UTC](https://www.ruby-forum.com/t/functional-testing-using-post-w-a-different-controller/48272/2 "2005-12-19T08:22:33Z")

</div>

doug wrote:

> Hallo – I have a function in test\_helper.rb that looks not a little  
> like this:
> 
> def  
> login(username=“gowondapiddlehead”,unhashed\_password=“gowonda\_piddlehead”)  
> post(:login,:system\_user=\>{:username=\>username,:unhashed\_password=\>unhashed\_password})  
> assert\_redirected\_to(:controller=\>‘home’)  
> assert\_not\_nil(session[:system\_user\_id])  
> system\_user=SystemUser.find(session[:system\_user\_id])  
> assert\_equal(username,system\_user.username,“Login name should match  
> session name.”)  
> end
> 
> When I run the test, however, I get the following error:
> 
> ```
> 1) Error:
> 
> ```
> 
> test\_index(HomeControllerTest):  
> NoMethodError: undefined method `login' for Test::Unit::TestCase:Class test/functional/home_controller_test.rb:18:in`test\_index’
> 
> I guess this is because I need to specify the controller being called as  
> well as the action in the post() function.  
> Does anyone know how to do this?  
> Cheers,  
> doug.

Doug,  
I’m having the exact same problem. I have an account controller that  
handles the login cycle. In testing another controller that depends on  
this cycle I cannot specify the controller, only the action.  
The option to specify another controller should be added.  
Does anyone know a work-around?

Zack

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [December 19, 2005, 9:01am UTC](https://www.ruby-forum.com/t/functional-testing-using-post-w-a-different-controller/48272/3 "2005-12-19T09:01:27Z")

</div>

On Dec 19, 2005, at 2:22 AM, Zack wrote:

> I’m having the exact same problem. I have an account controller  
> that  
> handles the login cycle. In testing another controller that depends on  
> this cycle I cannot specify the controller, only the action.  
> The option to specify another controller should be added.  
> Does anyone know a work-around?

The functional test operators take a session argument, where you can set  
the session up. Just test the pages by setting the session that  
indicates  
a login…

```
 get :show, # <- method
     { }, # <- params
     { :user_id => 4 } # <- session

```

–  
– Tom M.
