gpt4 book ai didi

ruby-on-rails - Heroku 生产环境上的 FactoryBot

转载 作者:太空宇宙 更新时间:2023-11-03 18:04:49 24 4
gpt4 key购买 nike

我正在尝试在 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 组之外:

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 server 时出错:

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获取更多信息。

关于ruby-on-rails - Heroku 生产环境上的 FactoryBot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49324903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com