[rspec-users] Cucumber speed tips

Ben Mabey ben at benmabey.com
Sat Jan 3 21:12:32 EST 2009


On 1/3/09 6:41 PM, Luke Melia wrote:
> On Dec 27, 2008, at 3:08 AM, Mischa Fierer wrote:
>
>> You only need to test login for each login case, not for each time 
>> you are testing some sort of logged in functionality.
>> In most cases you can just post to sessions/create rather than 
>> filling in a login form.
>
> I also wanted to speed up the "Given I am a logged in user" type of 
> thing, as you suggested, but took a different, faster approach which 
> may also work for you. I set a cookie to auto-login the user on the 
> next request. Implementation as a gist http://gist.github.com/42973 
> and below. -Luke
>
> features/steps/users_steps.rb:
>
> Given "I'm a logged in member" do
>   @me = create_adult
>   logged_in_as @me
> end
>
> features/support/env.rb:
>
> class Cucumber::Rails::World
>   ...
>   def logged_in_as(user)
>     cookies['integration_test_user'] = user.id.to_s
>   end
>   ,,,
> end
>
> app/controllers/application.rb
>
> class ApplicationController < ActionController::Base
>   ...
>   before_filter :login_integration_test_user, :if => lambda { 
> Rails.env == 'test' }
>   ...
>   def login_integration_test_user
>     return true if cookies['integration_test_user'].blank?
>     integration_test_user_id = cookies['integration_test_user'].to_i
>
>     if integration_test_user_id != current_user.id
>       reset_session
>       self.current_user = User.find(integration_test_user_id)
>     end
>
>     cookies['integration_test_user'] = nil
>   end


The downside with this approach is that it only works with the rails 
webrat adapter.  One solution which I have been meaning to do is to 
create a UsersSessionManager.  The manager would be responsible for 
logging in all the various roles you use in your app with different 
webrat sessions (and then caching them.)  You could then swap out which 
session you would be using in your steps...

So you could then do something like:

logged_in_as 'Admin' do |session|
  session.click_link 'Foo'
...
end

Or..

Given /^I am logged in as an '(.+)'$/ |role|
   login_as role
end

The 'login_as' would swap out the session that the World object uses so 
the next steps would be using the appropriate session.

WDYT?

-Ben


-Ben



More information about the rspec-users mailing list