# Functional Test Problem. Nubee, please help

**URL:** <https://www.ruby-forum.com/t/functional-test-problem-nubee-please-help/59019>\
**Category:** Rails\
**Created:** [April 27, 2006, 10:44pm UTC](https://www.ruby-forum.com/t/functional-test-problem-nubee-please-help/59019 "2006-04-27T22:44:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![allen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@allen](https://www.ruby-forum.com/u/allen)\
**Post date:** [April 27, 2006, 10:44pm UTC](https://www.ruby-forum.com/t/functional-test-problem-nubee-please-help/59019/1 "2006-04-27T22:44:14Z")

</div>

ALl my controllers require the user to be logged in. SO they will be  
redirected to my “login” controller.

How do i login first in a functional test? Im assuming i use the setup  
method to login the controller.

This is the setup method for the Activities controller functional test:

def setup  
@controller = ActivitiesController.new  
@request = ActionController::TestRequest.new  
@response = ActionController::TestResponse.new  
end

my “login” controller has a method “authenticate” which needs  
params[:username] and params[:password]. Please help!

Thanks  
Allen

---

<div class="post-metadata">

**Author:** ![allen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@allen](https://www.ruby-forum.com/u/allen)\
**Post date:** [April 27, 2006, 11:14pm UTC](https://www.ruby-forum.com/t/functional-test-problem-nubee-please-help/59019/2 "2006-04-27T23:14:41Z")

</div>

On Apr 27, 2006, at 04:44 PM, Allen wrote:

> def setup  
> @controller = ActivitiesController.new  
> @request = ActionController::TestRequest.new  
> @response = ActionController::TestResponse.new  
> end
> 
> my “login” controller has a method “authenticate” which needs  
> params[:username] and params[:password]. Please help!

You can pass several hashes to all of the request methods: params,  
session and flash. From the AWDR chapter on Testing, specifically  
Testing Controllers:

get/post/put/delete/head(action, parameters=nil, session=nil,  
flash=nil)

Examples:  
get :index  
post :login, :user =\> {:name =\> “fred”, :password =\> “abracadabra”}

-Brian

---

<div class="post-metadata">

**Author:** ![allen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@allen](https://www.ruby-forum.com/u/allen)\
**Post date:** [April 27, 2006, 11:27pm UTC](https://www.ruby-forum.com/t/functional-test-problem-nubee-please-help/59019/3 "2006-04-27T23:27:54Z")

</div>

Brian H. wrote:

> On Apr 27, 2006, at 04:44 PM, Allen wrote:
> 
> > def setup  
> > @controller = ActivitiesController.new  
> > @request = ActionController::TestRequest.new  
> > @response = ActionController::TestResponse.new  
> > end
> > 
> > my “login” controller has a method “authenticate” which needs  
> > params[:username] and params[:password]. Please help!
> 
> You can pass several hashes to all of the request methods: params,  
> session and flash. From the AWDR chapter on Testing, specifically  
> Testing Controllers:
> 
> get/post/put/delete/head(action, parameters=nil, session=nil,  
> flash=nil)
> 
> Examples:  
> get :index  
> post :login, :user =\> {:name =\> “fred”, :password =\> “abracadabra”}
> 
> -Brian

But if i am in my ActivitiesController functional test, then a call to :

post :login, :user =\> {:name =\> “fred”, :password =\> “abracadabra”}

will post to the login action of ActivitiesController and not  
LoginController. Do you see my problem?

---

<div class="post-metadata">

**Author:** ![allen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@allen](https://www.ruby-forum.com/u/allen)\
**Post date:** [April 27, 2006, 11:45pm UTC](https://www.ruby-forum.com/t/functional-test-problem-nubee-please-help/59019/4 "2006-04-27T23:45:10Z")

</div>

On Apr 27, 2006, at 05:27 PM, Allen wrote:

> But if i am in my ActivitiesController functional test, then a call  
> to :
> 
> post :login, :user =\> {:name =\> “fred”, :password =\> “abracadabra”}
> 
> will post to the login action of ActivitiesController and not  
> LoginController. Do you see my problem?

Well, yes and no. Obviously, if you aren’t in the LoginController,  
you don’t want to be sending a post to one of its actions.

But, in order to test your ActivitiesController you need to be an  
already logged in user when you call the action. As long as your  
LoginController sets one, or more, values in the session object that  
signifies you are logged in, then you don’t really have a problem.  
Simply pass those same values as part of the get call:

get :index, {}, {:logged\_in =\> true}

Where the second hash that you pass to get/post/put/delete/head  
contains the values you want to seed into the session object…

-Brian

---

<div class="post-metadata">

**Author:** ![allen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@allen](https://www.ruby-forum.com/u/allen)\
**Post date:** [April 27, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/functional-test-problem-nubee-please-help/59019/5 "2006-04-27T23:48:15Z")

</div>

On 4/27/06, Allen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> @request = ActionController::TestRequest.new
> @response = ActionController::TestResponse.new
> 
> ```
> 
> end
> 
> my “login” controller has a method “authenticate” which needs  
> params[:username] and params[:password]. Please help!
> 
> Thanks  
> Allen

Since you’re going to be testing the actual login process in the  
functional tests for your login controller, you might as well just  
bypass it elsewhere.

In the setup method, you can say:  
@request.session[:user] = User.new :name =\> ‘Superhacker’  
(or whatever the particulars of your login system require.)

---

<div class="post-metadata">

**Author:** ![allen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@allen](https://www.ruby-forum.com/u/allen)\
**Post date:** [April 27, 2006, 11:39pm UTC](https://www.ruby-forum.com/t/functional-test-problem-nubee-please-help/59019/6 "2006-04-27T23:39:05Z")

</div>

Hi,  
You can pass an object as a third parameter that contains the data that  
would be in you session.  
Since I’m using activeRbac all I need is a rbac\_user with a couple roles  
to get past my login system.

* * *

fixtures :users, :roles

def test\_index  
get :index, {}, {:rbac\_user =\> get\_user}  
assert\_response :success  
assert\_template ‘index’  
end

private

## def get\_user user = users(:testuser001) user.roles.push roles(:Admin) user.roles.push roles(:User) end

- Eric

Allen wrote:

> ```
> @request = ActionController::TestRequest.new
> @response = ActionController::TestResponse.new
> 
> ```
> 
> end
> 
> my “login” controller has a method “authenticate” which needs  
> params[:username] and params[:password]. Please help!
> 
> Thanks  
> Allen

–  
Eric G.  
[http://www.ericgoodwin.com](http://www.ericgoodwin.com)

---

<div class="post-metadata">

**Author:** ![allen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@allen](https://www.ruby-forum.com/u/allen)\
**Post date:** [April 27, 2006, 11:59pm UTC](https://www.ruby-forum.com/t/functional-test-problem-nubee-please-help/59019/7 "2006-04-27T23:59:10Z")

</div>

Allen wrote:

> ALl my controllers require the user to be logged in. SO they will be  
> redirected to my “login” controller.
> 
> How do i login first in a functional test? Im assuming i use the setup  
> method to login the controller.

Functional tests are really unit tests for a controller. They’re really  
designed to test a single controller in isolation.

It sounds like you might want to look at integration tests, new with 1.1  
(I think)

[http://jamis.jamisbuck.org/articles/2006/03/09/integration-testing-in-rails-1-1](http://jamis.jamisbuck.org/articles/2006/03/09/integration-testing-in-rails-1-1)

Alan
