我在运行测试文件时遇到错误。
factory.rb:334:in `factory_by_name': No such factory: user (ArgumentError)
我有用户模型,但仍然会收到此错误的原因? 我的factories.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' 宝石'摩卡' 端
答案 0 :(得分:1)
将您的目录重命名为以下之一:
可以在任何地方定义工厂,但如果是,则会自动加载 它们在以下位置的文件中定义:
test / factories.rb
规格/ factories.rb
测试/工厂/ *。RB
规格/工厂/ *。RB
https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md