Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise helpers are not available in cucumber step definitions

I'm using cucumber to test my rails 3.1 application and i'm using devise for authentication. I need to test if current user email is the same with the one used during authorization. The problem is that devise helpers are not available in cucumber step definition.

Is there any solution to make devise helpers available in cucumber step definitions?

i tried this:

Cucumber::Rails::World.send('define_method', 'current_user') do
    @current_user ||= (session[:user] && User.find(session[:user])) || :false
end

but that didn't help.

like image 848
roman Avatar asked Oct 31 '11 14:10

roman


1 Answers

Add this to the step definitions file that needs to use it:

World(Devise::TestHelpers)

While this makes the 'sign_in' and 'sign_out' methods available, they can't actually be used from Cucumber, so this DOES NOT WORK. The comments at the top of devise's file says "Do not use Devise::TestHelpers in integration tests". See: https://github.com/plataformatec/devise/blob/master/lib/devise/test_helpers.rb

Unfortunately, cucumber steps don't have access to the web request object and session parameters. It appears the only way to do this is to repeatedly perform the login web requests at the start of each scenario. Boring. I find it odd that cucumber can directly manipulate the test rails app's database but not the web request/session. In any case, this old post shows a good example of refactoring steps to be in a module that can be shared by many steps easily: http://drnicwilliams.com/2009/04/15/cucumber-building-a-better-world-object/

If anyone else has an answer for how a scenario can set the logged in state without having to do all the capybara web calls, I'd love to hear it.

like image 113
Matt Connolly Avatar answered Jan 01 '23 13:01

Matt Connolly