- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试在 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/
我想像这样使用 FactoryBot 随机返回特征: FactoryBot.define do factory :user do [:active, inactive].sample
在 5.0 FactoryBot 版本中,静态属性将被弃用,必须使用动态属性。所以: factory :product do name 'Some Product' end 需要变成: facto
我在测试中使用 factory_bot 创建对象,这里是我的工厂示例: factory :user do name "John" surname "Doe" trait :with_pho
我使用 factorybot有时在开发中创建记录。但是,它创建了一堆我没有预料到的额外数据。 我有两个工厂: FactoryBot.define do factory :user do f
我相信最好创建一个新问题...它遵循 my previous question我的模型产品有多种尺寸(嵌套属性) 我想创建工厂,但我不能让它工作...... 如果产品至少有一个尺寸( size_nam
我在 Rails 上使用 FactoryBot 和 Rspec。 我有一个 具体关键字 我扩展了 inittialize 的 ruby 类方法: def initialize(args) s
我正在学习如何在 Rails 中进行测试,并且正在为我的问题模型编写一个工厂: require 'factory_bot' FactoryBot.define do factory :questi
我有工厂 FactoryBot.define do factory :activity_fit_file do association :user, factory: :use
我有一个现有的工厂(订单),我正在尝试创建一个有效继承它的新工厂。它看起来像这样: factory :order_with_domain, :parent => :order do |o| o.o
是否有 FactoryBot 方法或某种方式来获取工厂的可用特征? 例如: FactoryBot.define do factory :address, class: Address do
给定两个模型,Alert 和 Zipcode,其中一个 Alert 必须有 1 个或多个 Zipcode: class Alert { :minimum => 1 } end class Zipco
当在 Gemfile 的开发和测试 block 中包含 factory_bot_rails gem 时,rails 将在生成模型时自动生成工厂。 有没有办法在生成模型后生成工厂? 注:FactoryB
在我的模型中,我有一个具有默认值的属性 foo。 我想编写一个与 foo 默认值相关的测试,但我的 FactoryBot 将其设置为 nil(并覆盖了默认值)。 如果我从工厂机器人中删除 foo 它会
我需要生成看起来像“ABC1”和“XYZ9”的标签序列——总是三个字母后跟一个数字。我希望标签的两个部分都按逻辑递增,并且始终保持准确的格式和长度。 我开始: sequence(:code) { |n
我有一个从休息 API 获取信息的对象。假设我从 API 调用 Posts,我可以指定要提取的字段。只是它的外观示例: Post.find!(id: 1, fields: [:id, :title,
我正在尝试在 heroku 中使用 gem FactoryBot.. 在本地一切正常,工厂已正确找到。 我正在尝试在 spec 文件夹之外使用工厂。 我在服务 invoice.rb 中使用它: cla
使用 Ruby gem 并尝试在 RSpec 中使用 FactoryBot。 我在 support/factory_bot.rb 中有这个: RSpec.configure do |config|
当我运行用户规范时,我收到 FactoryBot::DuplicateDefinitionError:Factory alreadyregistered:user 错误。我想创建多个具有不同名称和年龄
有人可以告诉我我的设置方式是否错误吗? 我有以下具有 has_many.through 关联的模型: class Listing :listing_features validates_pres
我遇到了有线问题,factoryBot 正在 Db 中创建记录,但是当 capybara 尝试访问它时,HTML 中没有记录。 我在提示时尝试使用“byebug”进行调试,当我说@topics =>
我是一名优秀的程序员,十分优秀!