FactoryBot on heroku 生产环境



我正在尝试在heroku使用gem FactoryBot。
在当地一切正常,工厂被正确找到。
我正在尝试使用规范文件夹之外的工厂。

我在服务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 组之外:

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)>'

我有人成功地在生产模式下使用工厂机器人吗?

默认情况下,factory_bot不会在任何地方都包含自身,仅在您通常使用它的规范中。

尝试做

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。

最新更新