我正在尝试使用像这样的rake任务中的Factory Girl:
require 'factory_girl'
require File.expand_path("spec/factories.rb")
namespace :users do
desc "Create sample users for use in development"
task :create_sample_users => :environment do
Factory(:user, :email => "pending@acme.com")
Factory(:approved_user, :email => "user@acme.com")
end
end
但是,当我运行rake users:create_sample_users
时,我收到错误uninitialized constant Entry
(条目是我的某个应用类的名称)。
谁能告诉我如何让工厂女孩看我的课程?它在我的测试中运行良好,只是在我的rake任务中失败。
答案 0 :(得分:10)
我猜测Rails没有在您需要工厂时加载模型。试试这个:
require 'factory_girl'
namespace :users do
desc "Create sample users for use in development"
task :create_sample_users => :environment do
require File.expand_path("spec/factories.rb")
Factory(:user, :email => "pending@acme.com")
Factory(:approved_user, :email => "user@acme.com")
end
end
答案 1 :(得分:0)
@ dmcnally的回答对我不起作用,因为我发现了未找到常量的奇怪错误。相反,我通过炮击rails runner来解决它:
sh "rails runner 'FactoryGirl.create :user'"
答案 2 :(得分:0)
对于已替换factory_bot
的{{1}},请使用:
factory_girl