在Cucumber功能中使用Factory Girl步骤定义(Rails 3)

Dor*_*ian 7 cucumber ruby-on-rails-3 factory-bot

我正在尝试使用Cucumber和Factory Girl.以下行:

  Given I am not logged in
  And   the following user exists:
    | login  | email               | password   | confirmation |
    | user50 | user50@mydomain.com | secret50   | secret 50    | 
 ....
Run Code Online (Sandbox Code Playgroud)

引发以下错误:

Undefined step: "the following user exists:" (Cucumber::Undefined exception)
/home/user/RubymineProjects/project/features/sign_in.feature:9:in `And the following user exists:

You can implement step definitions for undefined steps with these snippets:
And /^the following user exists:$/ do |table|
  # table is a Cucumber::Ast::Table
  pending # express the regexp above with the code you wish you had
end
Run Code Online (Sandbox Code Playgroud)

"

我已经安装了factory_girl_rails(甚至RubyMine的代码完成功能也适用于Factory Girl步骤......)

#Gemfile
group :test do
   gem "cucumber-rails", ">= 0.3.2"
   gem "factory_girl_rails"
 end

#features/support/env.rb
require 'factory_girl'
require 'factory_girl/step_definitions'
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?谢谢


更新:感谢@ sj26和@twmills我意识到我忘了创建一个带有Factory Girl的用户工厂.一旦我创建它,一切都运作良好.

Mik*_*Mik 11

对于那些现在试图使用FactoryGirl助手的人:

从FactoryGirl 3.5.0开始,这些步骤助手在4.0.0中被弃用并删除:http://robots.thoughtbot.com/post/25650434584/writing-better-cucumber-scenarios-or-why-were

从FactoryGirl 3.5.0开始,使用FactoryGirl生成的任何步骤定义将打印出弃用警告.我们将根据SemVer在FactoryGirl的4.0.0版本中完全删除步骤定义.我想现有的代码将被提取到一个类似于Cucumber Rails的训练轮的宝石上,并带有一个很好的警告,敦促开发人员不要使用这些步骤.

因此,如果您想在Cucumber中使用FactoryGirl,您应该在自己的步骤定义中使用它.


sj2*_*j26 7

您需要先包含工厂.factory_girl/step_definitions将迭代您定义的工厂,为每个工厂定义一个步骤.

我用这个features/support/factory_girl.rb:

# Require factories...
require 'spec/factories'

# Then define steps based on factories.
require 'factory_girl/step_definitions'
Run Code Online (Sandbox Code Playgroud)