我试图在heroku使用gem FactoryBot ..
在当地一切正常,工厂得到妥善找到
我正在尝试在spec文件夹外使用工厂。
我在服务invoice.rb
中使用它:
class InvoiceService
@invoices ||= FactoryBot.build_list :invoice, 50, :with_invoice_positions
end
这是我的发票工厂的样子:
FactoryBot.define do
factory :invoice do
...
trait :with_invoice_positions do
transient do
invoice_positions_count 5
end
after(:build) do |invoice, evaluator|
...
end
end
end
end
最后一件事是通过Gemfile将FactoryBot行放在:test group:
之外ruby '2.5.0'
source 'https://rubygems.org'
gem 'rails', '~> 5.1.4'
gem 'pg', '~> 0.18'
gem 'puma', '~> 3.7'
...
gem 'factory_bot_rails', '~> 4.8.2'
gem 'faker', git: 'https://github.com/stympy/faker.git', branch: 'master'
...
group :production do
gem 'rails_12factor', '~> 0.0.3'
end
group :development, :test do
...
end
group :development do
...
end
group :test do
...
end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
尝试在heroku上运行rails服务器时出错:
2018-03-16T13:13:16.816776+00:00 app[web.1]: Exiting
2018-03-16T13:13:16.816856+00:00 app[web.1]: /app/vendor/bundle/ruby/2.5.0/gems/factory_bot-4.8.2/lib/factory_bot/registry.rb:24:in `find': Factory not registered: invoice (ArgumentError)
2018-03-16T13:13:16.816893+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.5.0/gems/factory_bot-4.8.2/lib/factory_bot/decorator.rb:10:in `method_missing'
2018-03-16T13:13:16.816894+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.5.0/gems/factory_bot-4.8.2/lib/factory_bot.rb:100:in `factory_by_name'
2018-03-16T13:13:16.816895+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.5.0/gems/factory_bot-4.8.2/lib/factory_bot/factory_runner.rb:12:in `run'
2018-03-16T13:13:16.816898+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.5.0/gems/factory_bot-4.8.2/lib/factory_bot/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
2018-03-16T13:13:16.816904+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.5.0/gems/factory_bot-4.8.2/lib/factory_bot/strategy_syntax_method_registrar.rb:32:in `block (2 levels) in define_list_strategy_method'
2018-03-16T13:13:16.816905+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.5.0/gems/factory_bot-4.8.2/lib/factory_bot/strategy_syntax_method_registrar.rb:32:in `times'
2018-03-16T13:13:16.816906+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.5.0/gems/factory_bot-4.8.2/lib/factory_bot/strategy_syntax_method_registrar.rb:32:in `each'
2018-03-16T13:13:16.816909+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.5.0/gems/factory_bot-4.8.2/lib/factory_bot/strategy_syntax_method_registrar.rb:32:in `map'
2018-03-16T13:13:16.816910+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.5.0/gems/factory_bot-4.8.2/lib/factory_bot/strategy_syntax_method_registrar.rb:32:in `block in define_list_strategy_method'
2018-03-16T13:13:16.816912+00:00 app[web.1]: from /app/app/services/invoice_service.rb:2:in `<class:InvoiceService>'
2018-03-16T13:13:16.816913+00:00 app[web.1]: from /app/app/services/invoice_service.rb:1:in `<top (required)>'
我有人成功地在生产模式下使用工厂机器人吗?
答案 0 :(得分:2)
尝试
require 'factory_bot'
FactoryBot.find_definitions
在invoice.rb
如果可行,您可以在初始化程序中添加factory_bot.rb
文件,并使用相同的内容在rails应用程序启动时加载(取决于您的偏好)
有关详细信息,请参阅https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#using-without-bundler。