Как аутентифицировать пользователя перед тестом на огурец? - PullRequest
0 голосов
/ 16 июня 2019

Я пытаюсь обернуть голову вокруг BDD, используя Cucumber (для функциональных тестов) и RSpec (для модульных тестов).

У меня проблемы с написанием моего первого теста на огурец. Сначала мне нужно пройти аутентификацию, но не знаю как.

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: 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

Я ожидаю, что тест пройдёт сейчас. Но, похоже, на странице входа появляется сообщение «Вам необходимо войти или зарегистрироваться перед продолжением».

1 Ответ

0 голосов
/ 07 июля 2019

Это сработало

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: 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
...