XP on Rails Extreme Programming Blog

7Dec/092

Add current_user to Cucumber step definitions

Cucumber provides a great way to apply BDD during your rails development.

Sometimes you need to populate your testing database with background data and sometimes you need to access the

current_user

method.

Generally

current_user

is a method defined in the

ApplicationController

class or in a user related module.

So it’s not accessible to Cucumber’s scenarios: let’s add it and share to all step definition’s classes.

#authentication_steps.rb
module AuthenticationHelpers
  def current_user_session
    return @current_user_session if defined (@current_user_session)
    @current_user_session = UserSession.find
  end

  def current_user
    return @current_user if defined?(@current_user)
    @current_user = current_user_session && current_user_session.user
  end
end
World(AuthenticationHelpers)
Tagged as: , 2 Comments