简体   繁体   中英

How do I Authenticate user before cucumber test?

I am trying to wrap my head around BDD using Cucumber (for feature tests) and RSpec (for Unit Tests).

I am having trouble writing my first test for Cucumber. I need to authenticate first, but don't know how.

Given(/^A logged in user$/) do
    visit new_user_session_path
    fill_in "Email Address", :with => "sebhastien@gibosse.com"
    fill_in "Password", :with => "123456"
    click_button "Log In"
end

When(/^I go to the teams page$/) do
    visit teams_path
end

Then(/^I should see a list of my teams$/) do
    expect(page).to have_content("Teams#index")
end

Feature:

Feature: Hello Cucumber
    As a user
    I want to see a list of teams on the Teams page
    So that I can manage them

    Background: User is Authenticated
        Given A logged in user

    Scenario: User sees teams
        When I go to the teams page
        Then I should see a list of my teams

I expect the test to pass now. But it seems that it is getting the "You need to sign in or sign up before continuing" on the sign-in page.

This worked

Given (/^I am not authenticated$/) do
    visit('/users/sign_out') # ensure that at least
end

Given (/^I am a new, authenticated user$/) do
    email = 'testing@man.net'
    password = 'secretpass'
    User.new(:email => email, :password => password, :password_confirmation => password).save!

    visit '/users/sign_in'
    fill_in "user_email", :with => email
    fill_in "user_password", :with => password
    click_button "Log In"

end

When(/^I go to the teams page$/) do
    visit teams_path
end

Then(/^I should see a list of my teams$/) do
    expect(page).to have_content("Teams#index")
end

Feature:

Feature: Hello Cucumber
    As a user
    I want to see a list of teams on the Teams page
    So that I can manage them

    Background: User Authenticates
        Given I am not authenticated
        Given I am a new, authenticated user

    Scenario: User sees teams
        When I go to the teams page
        Then I should see a list of my teams

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM