Ruby on rails 基于heroku生产环境的FactoryBot

Ruby on rails 基于heroku生产环境的FactoryBot,ruby-on-rails,ruby,heroku,ruby-on-rails-5,factory-bot,Ruby On Rails,Ruby,Heroku,Ruby On Rails 5,Factory Bot,我正在尝试使用heroku的gem FactoryBot…. 在当地,一切正常,工厂都能找到。 我正在尝试使用“工厂外部规范”文件夹 我在服务中使用它发票.rb: class InvoiceService @invoices ||= FactoryBot.build_list :invoice, 50, :with_invoice_positions end 下面是我的发票工厂的外观: FactoryBot.define do factory :invoice do ...

我正在尝试使用heroku的gem FactoryBot….
在当地,一切正常,工厂都能找到。
我正在尝试使用“工厂外部规范”文件夹

我在服务中使用它
发票.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行置于:测试组之外:

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)>'
2018-03-16T13:13:16.816776+00:00应用程序[web.1]:退出
2018-03-16T13:13:16.816856+00:00应用程序[web.1]:/app/vendor/bundle/ruby/2.5.0/gems/factory\u bot-4.8.2/lib/factory\u bot/registry.rb:24:“查找”:工厂未注册:发票(ArgumentError)
2018-03-16T13:13:16.816893+00:00应用程序[web.1]:from/app/vendor/bundle/ruby/2.5.0/gems/factory\u bot-4.8.2/lib/factory\u bot/decorator.rb:10:in'method\u missing'
2018-03-16T13:13:16.816894+00:00应用程序[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应用程序[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应用程序[web.1]:from/app/vendor/bundle/ruby/2.5.0/gems/factory\u bot-4.8.2/lib/factory\u bot/strategy\u syntax\u method\u registrator.rb:20:“定义中的块中的单个策略\u策略\u方法”
2018-03-16T13:13:16.816904+00:00应用程序[web.1]:from/app/vendor/bundle/ruby/2.5.0/gems/factory\u bot-4.8.2/lib/factory\u bot/strategy\u syntax\u method\u registrator.rb:32:define\u list\u strategy\u method中的“块(两级)”中
2018-03-16T13:13:16.816905+00:00应用程序[web.1]:from/app/vendor/bundle/ruby/2.5.0/gems/factory\u bot-4.8.2/lib/factory\u bot/strategy\u syntax\u method\u register.rb:32:in'times'
2018-03-16T13:13:16.816906+00:00应用程序[web.1]:from/app/vendor/bundle/ruby/2.5.0/gems/factory\u bot-4.8.2/lib/factory\u bot/strategy\u syntax\u method\u register.rb:32:in'each'
2018-03-16T13:13:16.816909+00:00应用程序[web.1]:from/app/vendor/bundle/ruby/2.5.0/gems/factory\u bot-4.8.2/lib/factory\u bot/strategy\u syntax\u method\u register.rb:32:in'map'
2018-03-16T13:13:16.816910+00:00应用程序[web.1]:from/app/vendor/bundle/ruby/2.5.0/gems/factory\u bot-4.8.2/lib/factory\u bot/strategy\u syntax\u method\u register.rb:32:“定义列表中的块\u策略\u方法”
2018-03-16T13:13:16.816912+00:00应用程序[web.1]:from/app/app/services/invoice\u service.rb:2:in`'
2018-03-16T13:13:16.816913+00:00应用程序[web.1]:from/app/app/services/invoice\u service.rb:1:in`'

I是否有人在生产模式下成功使用了factory bot?

factory\u bot默认情况下不包含在任何地方,只包含在您通常使用它的规范中

试着做

require 'factory_bot'
FactoryBot.find_definitions
invoice.rb中

如果可行,您可以在初始值设定项中添加具有相同内容的
factory_bot.rb
文件,以便在rails应用程序启动时加载该文件(取决于您的首选项)


有关更多信息,请参阅

默认情况下,factory\u bot不会在任何地方都包含它自己,只在您通常使用它的规范中包含它

试着做

require 'factory_bot'
FactoryBot.find_definitions
invoice.rb中

如果可行,您可以在初始值设定项中添加具有相同内容的
factory_bot.rb
文件,以便在rails应用程序启动时加载该文件(取决于您的首选项)


有关更多信息,请参阅

出于纯粹的好奇,你为什么要在生产中使用factory bot?很简单,我将它与faker结合起来,并在产品上演示生成假数据,它还模拟从第三方API获取数据。这很有趣,你为什么要在产品中使用假数据?作为TDD实践的一部分,难道所有这些测试都不应该通过吗?他可能想为客户机设置一个演示实例@m1l05z您可能想调查种子是否更适合您的用例。出于纯粹的好奇,您为什么要在生产中使用factory bot?非常简单,我将其与faker结合,并在prod上生成虚假数据。它还模拟从第三方API获取数据。这非常有趣,你为什么要在prod中使用假数据呢?作为TDD实践的一部分,难道所有这些测试都不应该通过吗?他可能想为客户机设置一个演示实例@m1l05z您可能想调查种子是否不适合您的用例。我在gemfile
gem'factory\u bot\u rails'、“~>4.8.2”中也做了更改,要求:false
无论如何谢谢!我在gemfile
gem'factory\u bot\u rails'、“~>4.8.2”中也做了修改,要求:false
无论如何谢谢!