Functional testing multiple page actions


#1

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.


#2

On 11/22/05, List R. 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


#3

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?


#4

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?


#5

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


#6

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