【问题标题】:factory girl error工厂女孩错误
【发布时间】:2012-10-18 18:50:41
【问题描述】:

我在运行测试文件时遇到错误。

factory.rb:334:in `factory_by_name': No such factory: user (ArgumentError)

我有用户模型,但为什么仍然出现此错误? 我的 factory.rb 包含:

FactoryGirl.define do
  factory :user do |u|
    u.sequence(:email) {|n| "#{n}@email.com"}
    u.login 'Joe'
    u.password 'password'
    u.password_confirmation 'password'
    u.phone '1111111111'
    u.gender 'F'
    u.active '1'
    u.social_profile_active '1'
  end
end

test_helper.rb

ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'factory_girl'
#Factory.find_definitions

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
  #
  # Note: You'll currently still have to declare fixtures explicitly in integration tests
  # -- they do not yet inherit this setting
  #fixtures :all

  # Add more helper methods to be used by all tests here...
end

我的 ruby​​ 版本是 1.8.7 和 rails3.0.1

我的 gemfile 看起来像这样: 组:测试做 宝石“应该” 宝石'factory_girl_rails' 宝石“摩卡” 结束

【问题讨论】:

  • 你的工厂文件存储在哪里?
  • 它在测试文件/Factories/factories.rb中

标签: ruby-on-rails-3 factory-bot


【解决方案1】:

将您的目录重命名为以下之一:

工厂可以在任何地方定义,但会自动加载 它们在以下位置的文件中定义:

test/factories.rb

spec/factories.rb

测试/工厂/*.rb

规格/工厂/*.rb

https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md

【讨论】:

  • 还有一个疑问是我正在使用 ruby​​ 1.8.7 和 rails 301,当我安装 factory_girl_rails3 时,它显示一个错误,它可以用于 ruby​​ 1.9..我现在该怎么办?
  • 切换到 ruby​​ 1.9 或安装旧版本的 factory_girl_rails。如果它适用于我上面的解决方案,您尝试了吗?
  • 不,它不工作,我安装了 factory_girl_rails 1.2。但仍然面临同样的问题:(
  • 上面的目录可能区分大小写..你检查了吗?另外,如果您使用旧版本的 factory_girl_rails,则可能必须使用旧语法..
  • 我可以知道在哪里可以找到旧语法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多