【发布时间】:2019-11-24 21:13:42
【问题描述】:
我有以下三个模型:Product、Warehouse和Inventory
# app/models/product.rb
class Product < ApplicationRecord
has_many :inventories
has_many :warehouses, through: :inventories
end
# app/models/warehouse.rb
class Warehouse < ApplicationRecord
has_many :inventories
has_many :products, through: :inventories
end
# app/models/inventory.rb
class Inventory < ApplicationRecord
belongs_to :product
belongs_to :warehouse
end
我有这个工厂用于库存:
FactoryBot.define do
factory :inventory do
product { nil }
warehouse { nil }
item_count { 1 }
low_item_threshold { 1 }
end
end
我如何将这个工厂用于库存,或者我的其他工厂需要进行哪些更改,以便我可以拥有这样的规格?
RSpec.describe Inventory, type: :model do
it "has a valid factory" do
expect(FactoryBot.build(:inventory)).to be_valid
end
end
【问题讨论】:
标签: ruby-on-rails rspec factory-bot