在 rake 任务中使用factory_girl - 获取未初始化的常量
我正在尝试在这样的 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 => "[email protected]")
Factory(:approved_user, :email => "[email protected]")
end
end
但是,当我运行 rake users:create_sample_users
时,我收到错误 uninitializedconstant Entry
(Entry 是我的应用程序的课程之一)。
谁能告诉我如何让工厂女孩来看我的课程?它在我的测试中运行良好,只是在我的 rake 任务中失败了。
I'm trying to use Factory Girl in a rake task like this:
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 => "[email protected]")
Factory(:approved_user, :email => "[email protected]")
end
end
However when I run rake users:create_sample_users
I get the error uninitialized constant Entry
(Entry is the name of one of my app's classes).
Can anyone tell me how to get Factory girl to see my classes? It's working fine in my tests, just failing in my rake tasks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于已替换
factory_girl
的factory_bot
使用:For
factory_bot
which has replacedfactory_girl
use:@dmcnally 的答案对我不起作用,因为我收到了未找到常量的奇怪错误。相反,我通过向 Rails runner 解决这个问题:
@dmcnally's answer didn't work for me, as I was getting odd errors of constants not found. Instead, I resolved it by shelling out to rails runner:
我猜测 Rails 在您需要工厂时尚未加载您的模型。试试这个:
I'm guessing that Rails hasn't loaded your models at the point you are requiring the factories. Try this: