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

发布于 2024-11-07 20:45:05 字数 1222 浏览 2 评论 0原文

我正在尝试使用黄瓜和工厂女孩。以下几行:

  Given I am not logged in
  And   the following user exists:
    | login  | email               | password   | confirmation |
    | user50 | [email protected] | 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 创建一个:user 工厂。一旦我创建了它,一切都运行良好。

I'm trying to use Cucumber and Factory Girl. The following lines:

  Given I am not logged in
  And   the following user exists:
    | login  | email               | password   | confirmation |
    | user50 | [email protected] | secret50   | secret 50    | 
 ....

raises the following error:

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

'

I've installed factory_girl_rails (even RubyMine's code completion feature works with Factory Girl step ... )

#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'

Any ideas? Thanks


Update: Thanks to @sj26 and @twmills I realized that I forgot to create a :user factory with Factory Girl. Once I created it, everything worked well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

累赘 2024-11-14 20:45:05

对于那些现在尝试使用 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,您应该在您自己的步骤定义中使用它。

For those people, who trying to use FactoryGirl helpers now:

From FactoryGirl 3.5.0 these step helpers are deprecated and removed in 4.0.0: http://robots.thoughtbot.com/post/25650434584/writing-better-cucumber-scenarios-or-why-were

As of FactoryGirl 3.5.0, using any of FactoryGirl’s generated step
definitions will print out a deprecation warning. We’ll be removing
the step definitions completely in the 4.0.0 release of FactoryGirl in
accordance with SemVer. I imagine the existing code will be extracted
to a gem similar to Cucumber Rails’ training wheels with a nice
warning urging developers not to use the the steps.

So if you want to use FactoryGirl in Cucumber you should use it in your own step definitions.

怼怹恏 2024-11-14 20:45:05

您需要首先包括您的工厂。 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'

You need to include your factories first. factory_girl/step_definitions will iterate over your defined factories to define a step for each.

I use this in features/support/factory_girl.rb:

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

# Then define steps based on factories.
require 'factory_girl/step_definitions'
微凉徒眸意 2024-11-14 20:45:05

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

Try this in features/step_definitions/user_steps.rb

Given /^the following user exists:$/ do |users|
  users.hashes.each do |user|
    Factory(:user,user)
  end
end

Although you may want this instead:

Given /^the following users:$/ do |users|
  etc..
各自安好 2024-11-14 20:45:05

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

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

Ok, I get that ThoughtBot wants us to write better code, but as an upgrade crutch, we can just put this old file in features/step_definitions:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文