views:

71

answers:

1

I'm new at cucumber and capybara so maybe this is easy.

I'm using headers to check if a user is logged in or not and i'm having a problem when doing cucumber testing.

I use Capybara and Cucumber and a "add headers hack": http://aflatter.de/2010/06/testing-headers-and-ssl-with-cucumber-and-capybara/

The problem I have is that it only sets the header once in each feature story. So if I have a story that goes trough more than one step the header is gone and the user is no longer logged in.

An example story:

Given I am logged in as a superuser
And I have a database "23456789" that is not active
And I am on the home page
When I follow the "Delete" link for "23456789.sqlite"
Then I should see "Deleted the database"

In this story the "When I follow the "Delete" link for "23456789.sqlite" line will not work since the user is no longer logged in!

Have thought about using session or the before/after in cucumber.

Does someone have a clue on how to fix this?

+1  A: 

You can achieve this by passing the username in an environment variable:

When /^I am logged in as a superuser$/ do
  ENV['RAILS_TEST_CURRENT_USER'] = 'admin'
end

application_controller.rb:

before_filter :stub_current_user

def stub_current_user
  if Rails.env == 'cucumber' || Rails.env == 'test'
    if username = ENV['RAILS_TEST_CURRENT_USER']
      @current_user = User.find_by_username(username)
    end
  end
end
Leventix