在 Cucumber 功能中使用 Factory Girl 步骤定义 (Rails 3)
我正在尝试使用黄瓜和工厂女孩。以下几行:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于那些现在尝试使用 FactoryGirl 助手的人:
从 FactoryGirl 3.5.0 开始,这些步骤助手已被弃用并在 4.0.0 中删除: http://robots.thoughtbot.com/post/25650434584/writing-better-cucumber-scenarios-or-why-were
因此,如果您想在 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
So if you want to use FactoryGirl in Cucumber you should use it in your own step definitions.
您需要首先包括您的工厂。
factory_girl/step_definitions
将迭代您定义的工厂,为每个工厂定义一个步骤。我在
features/support/factory_girl.rb
中使用它: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
:在 features/step_definitions/user_steps.rb 中尝试一下,
尽管您可能需要这样:
Try this in features/step_definitions/user_steps.rb
Although you may want this instead:
好吧,我知道 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