1

运行测试文件时出现错误。

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

我的红宝石版本是 1.8.7 和 rails3.0.1

我的 gemfile 看起来像这样: group :test do gem 'should' gem 'factory_girl_rails' gem 'mocha' end

4

1 回答 1

1

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

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

测试/factories.rb

规格/工厂.rb

测试/工厂/*.rb

规格/工厂/*.rb

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

于 2012-10-29T12:55:46.593 回答