设计/黄瓜 - 添加确认用户的步骤

ben*_*itr 6 ruby-on-rails cucumber devise factory-bot

我是黄瓜新手,我发现以下片段来测试Devise登录功能.然而,似乎又失去了一步,我没有找到任何解决方案:

Given /^that a confirmed user exists$/ do
  pending # express the regexp above with the code you wish you had
end
Run Code Online (Sandbox Code Playgroud)

这里有以下代码:

功能/认证/ session.feature

Feature: Session handling
  In order to use the site
  As a registered user
  I need to be able to login and logout

Background: 
  Given that a confirmed user exists

Scenario Outline: Logging in
  Given I am on the login page
  When I fill in "user_email" with "<email>"
  And I fill in "user_password" with "<password>"
  And I press "Sign in"
  Then I should <action>
  Examples:
    |         email       |  password   |              action             |
    | minimal@example.com |  test1234   | see "Signed in successfully"    |
    | bad@example.com     |  password   | see "Invalid email or password" |

Scenario: Logging out
  Given I am logged in
  When I go to the sign out link
  Then I should see "Signed out successfully"
Run Code Online (Sandbox Code Playgroud)

特征/ step_definitions/authentication_steps.rb

# Session
Given /^I am logged in$/ do
  visit path_to('the login page')
  fill_in('user_email', :with => @user.email)
  fill_in('user_password', :with => @user.password)
  click_button('Sign in')
  if defined?(Spec::Rails::Matchers)
    page.should have_content('Signed in successfully')
  else
    assert page.has_content?('Signed in successfully')
  end
end
Run Code Online (Sandbox Code Playgroud)

规格/工厂/ user.rb

Factory.define :minimal_user, :class => User do |u|
  u.username 'minimal'
  u.email 'minimal@example.com'
  u.password 'test1234'
  u.password_confirmation 'test1234'
end
Run Code Online (Sandbox Code Playgroud)

这里是原始代码的链接

非常感谢您的帮助!!

Dan*_*del 3

您的标题说“验证用户存在”,但这可能不是您需要做的 - 您的Given步骤不需要断言某些内容有效,它们调用代码来为您的场景创建应用程序状态。当然,它们仍然是测试,因为它们仍然可能失败。

我想你正在寻找这样的东西:

Given /^that a confirmed user exists$/ do
  Factory.create(:minimal_user)
end
Run Code Online (Sandbox Code Playgroud)

这将从您的工厂定义中创建并保存一个新的确认用户,以便场景的其余部分可以继续。