5

What I what to accomplish is to use (rely on) current_user method while defining Cucumber steps. I'm using Clearance in my project.

First of all I tried to use sign_in but it didn't work (I guess Cucumber World doesn't know about Clearance methods...).

So how do I make Cuckes recognize current_user and sign_in/sign_out methods?

Bryan Ash
  • 4,385
  • 3
  • 41
  • 57
oldhomemovie
  • 14,621
  • 13
  • 64
  • 99

2 Answers2

6

Your Cucumber features should be driving your application through the public user interface. Something like:

Given /^I am signed in as "([^\"]*)"%/ do |username|
  visit 'sign_in'
  fill_in 'Username', :with => username
  click 'Sign In'
end

Since the current_user method isn't available to the browser, you shouldn't be using it in your spec.

You could fake it in your steps by storing @current_user in the above step and then providing an attribute reader for it.

Bryan Ash
  • 4,385
  • 3
  • 41
  • 57
  • After 9 months I realized how to cucke it right. Now understand why your answer was correct back in a day. – oldhomemovie Nov 15 '11 at 15:43
  • It's still frustrating though. Because `current_user` is `nil` even on the server side, not just the browser side. – Jason Kim Aug 06 '13 at 21:17
1

I disagree with the idea that every acceptance test (cucumber or otherwise) must exercise the login logic. Luckily, if you agree, Clearance has added a back door in tests that lets you skip the sign in steps.

user = create(:user)
visit posts_path(as: user)

Now you can leave your login-related features driving the login ui as a user would and skip that for features that aren't directly related to logging in.

Derek Prior
  • 3,497
  • 1
  • 25
  • 30