使用Cucumber和Rails 3设置Factory Girl

3

我正在尝试在Rails 3中安装Factory Girl,但是当我运行rake cucumber时出现以下错误:

james@james-laptop:~/rails-projs/simple-beach-63$ rake cucumber:wip
(in /home/james/rails-projs/simple-beach-63)
bundle exec /usr/bin/ruby1.8 -I "/usr/lib/ruby/gems/1.8/gems/cucumber-0.9.4/lib:lib"

"/usr/lib/ruby/gems/1.8/gems/cucumber-0.9.4/bin/cucumber" --profile wip Using the wip profile... uninitialized constant Factory (NameError) /usr/lib/ruby/gems/1.8/gems/rspec-expectations-2.1.0/lib/rspec/expectations/backward_compatibility.rb:6:in const_missing' /usr/lib/ruby/gems/1.8/gems/factory_girl-1.3.3/lib/factory_girl/step_definitions.rb:25 /home/james/rails-projs/simple-beach-63/features/support/env.rb:8:inrequire' /home/james/rails-projs/simple-beach-63/features/support/env.rb:8

您好,当您有时间时能否拉取并查看一下?

我在Gemfile中有以下内容:

gem 'factory_girl_rails'
gem 'factory_girl'

我在feature/support/env.rb中有这个内容。

require "factory_girl/step_definitions"
require "factory_girl"
require File.dirname(__FILE__) + "/factories"

然后我在features/support/factories.rb中定义了一个工厂。

我会非常感激任何帮助。


如果您使用factory_girl_rails gem会发生什么? - monocle
3个回答

8

您只需要执行以下步骤。

Gemfile:

group :development, :test do
  gem "rspec-rails"
end

group :test do
  gem "cucumber-rails"
  gem "factory_girl_rails"
end

features/support/factory_girl.rb:

require 'factory_girl/step_definitions'

spec/factories.rb:

# your Factory definitions.

3

丹,

我按照你的步骤走了,但是我仍无法使用factory girl的step definitions。

当我尝试:

  Given I am not logged in
  And   the following user exists:
    | login  | email               | password   | confirmation |
    | user50 | user50@mydomain.com | secret50   | secret 50    |
 ...

I get the following error:

Undefined step: "the following user exists:" (Cucumber::Undefined exception)

You can implement step definitions for undefined steps with these snippets:
Given /^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

有没有想法缺了什么?

你需要先在 features/support/factory_girl.rb 中 require 'factory_girl_rails',因为步骤定义是从你的工厂生成的。 - RobinGower

0
这里的问题在于您没有正确地调用表格。从您的特性文件中调用表格的行应该像这样:
And the following user with <login> and <email> <password> and <confirmation> exists

你的步骤定义应该像这样:

And /^The following user with ([A-za-z0-9\.@:]+) and ([A-za-z0-9\.@:]+) ([A-za-z0-9\.@:]+) and ([A-za-z0-9\.@:]+) exists$/ do |login, email, password, confirmation|

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接