ruby - 工厂女郎 : Factory not registered: user (ArgumentError)

标签 ruby cucumber factory-bot

在使用 FactoryGirl 将所有鸭子按正确顺序排列时遇到很多麻烦。

设置极简 Rails 应用程序 (3.0.11)、factory_girl_rails (1.4.0)、factory_girl (2.3.2) & cucumber-rails (1.2.1) 和 ruby​​-1.8.7-p352。

cucumber 测试是:

Feature: a
  Scenario: test factory-girl
    Given the following user exists:
    | name    | email               |
    | Brandon | brandon@example.com |

结果是这样的:

cucumber 
Using the default profile...
"Adam Advertiser"
"a@b.com"
#<User id: nil, name: nil, created_at: nil, updated_at: nil, email: nil>
Feature: a

  Scenario: test factory-girl        # features/users.feature:2
    Given the following user exists: # factory_girl-2.3.2/lib/factory_girl/step_definitions.rb:100
      | name    | email               |
      | Brandon | brandon@example.com |
      **Factory not registered: user (ArgumentError)**
      features/users.feature:3:in `Given the following user exists:'

Failing Scenarios:
cucumber features/users.feature:2 # Scenario: test factory-girl

1 scenario (1 failed)
1 step (1 failed)
0m0.107s

这似乎是一个加载顺序问题,也许吧。将不胜感激任何帮助,以做到这一点。 问候 罗斯

features/Factories.rb 因此:

FactoryGirl.define do
    factory :user do
        name 'Adam Advertiser'
        email 'a@b.com'
    end
end
pp FactoryGirl.create(:user)

require 'factory_girl/step_definitions'

我的 features/support/env.rb 是:

require 'pp'
require 'cucumber/rails'

require 'factory_girl_rails'

我的 GemFile 是:

source 'http://rubygems.org'

gem 'rails', '3.0.11'

gem 'sqlite3'


group :development, :test do

    gem 'cucumber-rails'
    gem 'factory_girl_rails'     # rails 3 version

end

最佳答案

require 'factory_girl_rails' 之后立即调用 FactoryGirl.find_definitions 为我解决了类似的问题。

参见 Cannot get factory_girl running under rails 3.0.5,unexpected tCONSTANT

关于ruby - 工厂女郎 : Factory not registered: user (ArgumentError),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8409787/

相关文章:

sql - 使用 Rails 在 Postgres 中开发自定义函数

ruby - local_variables 的行为 - 谁能解释一下?

ruby - FactoryGirl create_list 传入多个值

ruby-on-rails - Rspec 测试实例变量与用户创建

html - ruby 转储对象到 html 格式的字符串

ruby - 我需要一个正则表达式来捕获字符串中的数字

java - 在 Android 上使用 Cucumber 时出现 AmbigouslyStepDefinitionsException

ruby - 使用 ruby​​ capybara 获取页面的所有 href 链接

java - 命令行运行JAVA+Cucumber+JUnit Maven项目

ruby-on-rails - 为什么 Rails 不会在运行之间重置测试数据库