# Functional testing multiple page actions

**URL:** <https://www.ruby-forum.com/t/functional-testing-multiple-page-actions/48635>\
**Category:** Rails\
**Created:** [November 22, 2005, 9:47pm UTC](https://www.ruby-forum.com/t/functional-testing-multiple-page-actions/48635 "2005-11-22T21:47:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [November 22, 2005, 9:47pm UTC](https://www.ruby-forum.com/t/functional-testing-multiple-page-actions/48635/1 "2005-11-22T21:47:17Z")

</div>

Some actions in Rails require sessions spanning multiple pages. Things  
like “login & check account” or “fill a shopping cart and check out”.

I’m having trouble creating functional testing for these for two  
reasons:

1. The post() etc. methods seem to reset the session each time.

2. How do you create a functional test that accesses multiple  
controllers?

Specifically, could someone post a functional test to a controller that  
requires the user be logged in first?

Thanks.

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [November 22, 2005, 10:20pm UTC](https://www.ruby-forum.com/t/functional-testing-multiple-page-actions/48635/2 "2005-11-22T22:20:29Z")

</div>

On 11/22/05, List R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. How do you create a functional test that accesses multiple  
> controllers?
> 
> Specifically, could someone post a functional test to a controller that  
> requires the user be logged in first?
> 
> Thanks.

You can set the session yourself:

@request.session[:user] = users(:bob)

I went so far as to define a test helper method in test\_helper.rb:

login\_as :bob

def login\_as(user)  
@request.session[:user] = user.nil? ? nil : users(user)  
end

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [November 22, 2005, 10:38pm UTC](https://www.ruby-forum.com/t/functional-testing-multiple-page-actions/48635/3 "2005-11-22T22:38:16Z")

</div>

technoweenie wrote:

> You can set the session yourself:
> 
> @request.session[:user] = users(:bob)

I thought of doing that, but it couples the test to the session  
implentation, which I’d rather not do. Is there a better way?

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [November 23, 2005, 2:55am UTC](https://www.ruby-forum.com/t/functional-testing-multiple-page-actions/48635/4 "2005-11-23T02:55:21Z")

</div>

Maybe the best way to handle this is to make some type of test class,  
which would do the following, roughly:

class MockBroser  
attr\_reader :response, :session, :flash, :cookies

def put(controller, action, \*params)  
…  
end

def get(controller, action, \*params)  
…  
end  
end

What does everyone say?

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [November 25, 2005, 3:36pm UTC](https://www.ruby-forum.com/t/functional-testing-multiple-page-actions/48635/5 "2005-11-25T15:36:17Z")

</div>

jarkko - we could debate the value of encapsulation and loose coupling  
in tests, but, regardless, what about more complex scenarios, such as:

- Add item to cart, update quantity (2 requests, spans a session, may  
involve 2 controllers)
- Add items to cart, clear cart
- Multipage form (wizards)
- etc. etc. etc.

jarkko wrote:

> On 22.11.2005, at 23.38, List R. wrote:
> 
> > technoweenie wrote:
> > 
> > > You can set the session yourself:
> > > 
> > > @request.session[:user] = users(:bob)
> > 
> > I thought of doing that, but it couples the test to the session  
> > implentation, which I’d rather not do. Is there a better way?
> 
> Why? You are testing your login functionality. Your login system has  
> some way to make sure that a user is logged in. Normally it depends  
> on sessions. So why not depend on that in your tests, it’s after all  
> an integrated part of your login system.
> 
> If you think you might change the system later on, just create a  
> method for authorizing a user and use that method in both your  
> controllers and tests.
> 
> //jarkko

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [November 23, 2005, 11:38am UTC](https://www.ruby-forum.com/t/functional-testing-multiple-page-actions/48635/6 "2005-11-23T11:38:31Z")

</div>

On 22.11.2005, at 23.38, List R. wrote:

> technoweenie wrote:
> 
> > You can set the session yourself:
> > 
> > @request.session[:user] = users(:bob)
> 
> I thought of doing that, but it couples the test to the session  
> implentation, which I’d rather not do. Is there a better way?

Why? You are testing your login functionality. Your login system has  
some way to make sure that a user is logged in. Normally it depends  
on sessions. So why not depend on that in your tests, it’s after all  
an integrated part of your login system.

If you think you might change the system later on, just create a  
method for authorizing a user and use that method in both your  
controllers and tests.

//jarkko
