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. -- Posted via http://www.ruby-forum.com/.
On 11/22/05, List Recv <listrecv-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.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
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? -- Posted via http://www.ruby-forum.com/.
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? -- Posted via http://www.ruby-forum.com/.
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 -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
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-- Posted via http://www.ruby-forum.com/.