jnicklas/turnip

Session persists between scenarios

novaluke opened this issue · 4 comments

With Turnip when I log in in the first scenario for a feature it causes me to be logged in for the second scenario as well (despite not running the login steps). However, when using the default RSpec syntax (feature "foo" do ... and scenario "bar" do ...) each scenario block is completely independent of the other scenarios' states. That is, the second scenario does not become logged in as a result of the first scenario's actions. This is also true when I use Cucumber for the tests - each scenario starts with a blank slate.

I would have expected that in Turnip, just like in other testing frameworks, each test would start with a blank slate. Have I missed some important configuration step? Is this a bug?


# spec/acceptance/signing_in.feature
Feature: Signing in
  In order to access my account
  As a registered user
  I want to be able to sign in
  Background:
    Given I am on the sign in page

  Scenario: User enters valid credentials
    Given I am a registered user
    When I enter my credentials correctly
    Then I am taken to the homepage with a welcome message

  Scenario: User enters invalid credentials
    When I enter invalid credentials
    Then I am kept on the sign in page with an error message
# spec/acceptance/steps/signing_in_steps.rb
module SigningInSteps
  step "I am a registered user" do
    @user = create(:user)
  end
  step "I am on the sign in page" do
    visit sign_in_path
  end
  step "I enter my credentials correctly" do
    fill_in("Email", with: @user.email)
    fill_in("Password", with: @user.password)
    click_on "Sign in"
  end
  step "I am taken to the homepage with a welcome message" do
    expect(page).to have_current_path(home_path)
    expect(page).to have_content "You are signed in, enjoy!"
    expect(page).to have_content "Welcome, #{@user.full_name}"
  end

  step "I enter invalid credentials" do
    fill_in("Email", with: "email") # Fails on this line
    fill_in("Password", with: "password")
    click_on "Sign in"
  end
  step "I am kept on the sign in page with an error message" do
    expect(page).to have_current_path(sign_in_path)
    expect(page).to have_content "Invalid username or email"
  end
end

RSpec.configure { |c| c.include SigningInSteps }

Failure message:

  1) Signing in User enters invalid credentials Given I am on the sign in page -> When I enter invalid credentials -> Then I am kept on the sign in page with an error message
     Failure/Error: fill_in("Email", with: "email")

     Capybara::ElementNotFound:
       Unable to find field "Email"

(this is because a logged in user is redirected to the dashboard page when they access the sign in page)

gongo commented

Hi @mayhewluke

Do you use turnip/capybara (or capybara/rspec) ?
If you use it, session is deleted each scenario.

I'm not sure what you mean by "use turnip/capybara". I had -r turnip/rspec in my .rspec as per the README, but I never saw any mention of turnip/capybara.

Adding -r turnip/capybara to my .rspec seems to have solved the problem, though - is this how it's supposed to work?

gongo commented

is this how it's supposed to work?

Yes. see: https://github.com/jnicklas/turnip#using-with-capybara

Ah, got it. I must have missed that somehow. Thanks!