kvd6sw
Last Updated: February 25, 2016
·
1.439K
· paulelliott
98292675163b09c369e097cbeef92a10

Speeding up cucumber

You can bypass the page loads and form filling by implementing a sign in backdoor with a little bit of code.

This assumes your sign in is governed by a SessionsController and you can sign in by passing a User instance into a sign in method. Adjust to match your authentication system.

#features/support/sign_in_backdoor.rb

class SessionsController
  def backdoor
    sign_in(User.find_by_email(params[:email]))
    redirect_to :dashboard
  end
end

MyRailsApp::Application.routes.tap do |routes|
  routes.disable_clear_and_finalize = true
  routes.draw do
    match 'backdoor', to: 'sessions#backdoor'
  end
end

Now you can add a helper to your cucumber environment and use the sign_in function anywhere in your steps.

#features/step_definitions/session_steps.rb

module SessionStepMethods
  def sign_in(user)
    visit "/backdoor?email=#{user.email}"
  end
end
World(SessionStepMethods)

Don't worry, it is totally safe. Since the backdoor is in features/support, it gets monkey patched in when running cucumber but not in real server instances.

Say Thanks
Respond

3 Responses
Add your response

6513
Afd42fff23928a20972d60744fcdca4a

Cool tip. I couldn't find documentation on #disable_clear_and_finalize though, what does it do?

It's also not clear from your writeup that the first snippet of code should be in 'features/support' until later on. You may want to include a file name in a comment to rectify that.

Excellent tip though. I'll definitely be using this on a project I've started.

over 1 year ago ·
6514
98292675163b09c369e097cbeef92a10

#disable_clear_and_finalize basically unlocks the route set so you can add to it. Normally you can't declare a route outside of the routes.rb.

over 1 year ago ·
9169
Default profile 3 normal

That's awesome! Helpful for integration tests with capybara.

over 1 year ago ·