黄瓜测试前如何验证用户身份?

时间:2019-06-16 20:07:52

标签: ruby-on-rails cucumber bdd

我正在尝试使用Cucumber(用于功能测试)和RSpec(用于单元测试)围绕BDD。

我无法为黄瓜编写第一个测试。我需要先进行身份验证,但不知道如何。

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 :(得分:0)

这有效

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