Forum: Ruby on Rails Functional testing multiple page actions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ccfe7d097475a32dc3ff78d6fc42c852?d=identicon&s=25 List Recv (lstrecv)
on 2005-11-22 21:47
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.
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 technoweenie (Guest)
on 2005-11-22 22:20
(Received via mailing list)
On 11/22/05, List Recv <listrecv@gmail.com> wrote:
>
> 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.

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
Ccfe7d097475a32dc3ff78d6fc42c852?d=identicon&s=25 List Recv (lstrecv)
on 2005-11-22 22:38
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?
Ccfe7d097475a32dc3ff78d6fc42c852?d=identicon&s=25 List Recv (lstrecv)
on 2005-11-23 02:55
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?
82476266af9d460415d8f1fc16bb54ed?d=identicon&s=25 Jarkko Laine (jarkko)
on 2005-11-23 11:38
(Received via mailing list)
On 22.11.2005, at 23.38, List Recv 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
Ccfe7d097475a32dc3ff78d6fc42c852?d=identicon&s=25 List Recv (lstrecv)
on 2005-11-25 15:36
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 Recv 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
This topic is locked and can not be replied to.