- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想像这样使用 FactoryBot 随机返回特征:
FactoryBot.define do
factory :user do
[:active, inactive].sample
trait :active do
active { true }
...
end
trait :inactive do
active { false }
...
end
end
end
这样做:
(1..5).map{ |e| FactoryBot.build(:user) }.map(&:active?)
=> [true, false, false, true, false]
其实是这样的:
FactoryBot.define do
factory :user do
active { [true, false].Sample }
name { "name-#{SecureRandom.uuid}" }
birthday { active == true ? rand(18..99).years.ago - rand(0..365).days.ago : nil }
preferred_contact_method { active == true ? %w(phone email).sample : nil }
activated_at { active == true ? rand(1..200).days.ago : nil }
contact_phone_number { preferred_contact_method == "phone" ? "+33XXXXXXXXX" : nil }
contact_email { preferred_contact_method == "email" ? "toto@tati.com" : nil }
end
end
这有可能吗?
最佳答案
在寻找最终答案时,我发现 trait
仅在给定 block 后执行。我将暂时分享我正在使用的代码示例,尽管如果您对其进行测试,您将只有活跃用户或不活跃用户,而不会同时拥有这两种用户。
您可以在两个 lambda 中导出特征逻辑,然后随机选择一个:
trait_active = lambda do |context|
context.active { true }
#...
end
trait_inactive = lambda do |context|
context.active { false }
# ...
end
FactoryBot.define do
factory :user do
trait :active do
trait_active.call(self)
end
trait :inactive do
trait_inactive.call(self)
end
trait :schrodinger do
[trait_active, trait_inactive].sample.call(self)
end
end
end
lambda中的context
属性在这里相当重要,大家可以看看more关于那个in this answer .
关于ruby - FactoryBot 的随机特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55268328/
我想像这样使用 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 =>
我是一名优秀的程序员,十分优秀!