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

时间:2011-05-18 13:39:09

标签: ruby-on-rails-3 cucumber 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    | 
 ....

引发以下错误:

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

我已经安装了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'

有什么想法吗?感谢


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

4 个答案:

答案 0 :(得分: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生成的任何步骤   定义将打印出弃用警告。我们将删除   完全在FactoryGirl的4.0.0版本中的步骤定义   按照SemVer。我想象现有的代码将被提取出来   一个类似于Cucumber Rails的宝石训练轮的宝石   警告敦促开发人员不要使用这些步骤。

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

答案 1 :(得分: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'

答案 2 :(得分:1)

在features / step_definitions / user_steps.rb

中尝试此操作
Given /^the following user exists:$/ do |users|
  users.hashes.each do |user|
    Factory(:user,user)
  end
end

虽然你可能想要这个:

Given /^the following users:$/ do |users|
  etc..

答案 3 :(得分:1)

好的,我知道ThoughtBot希望我们编写更好的代码,但作为升级包,我们可以将这个旧文件放在features / step_definitions中:

https://raw.github.com/thoughtbot/factory_girl/3.6.x/lib/factory_girl/step_definitions.rb