FactoryGirl:避免为 1-Model 单元测试创建多个模型实例
FactoryGirl: avoid creating multiple model instances for a 1-Model Unit Test
Rails 3.2.13, FactoryGirl, minitest-spec-rails.
我想为 InvoicedItem
模型定义一个工厂。这个InvoicedItem
属于一个(多态的):owner
。这个 owner
可以是 SpentItem
。为了创建 SpentItem
,我需要创建其他几条记录(PricingGroup
、PriceRatio
、Supplier
等)。这很快就变成了一场噩梦。
有没有办法在不使用现有模型的情况下在 FactoryGirl 中定义关联?
基本上,我不想实例化几个 SpentItem
相关模型来测试 InvoicedItem
。我只需要 InvoicedItem 的 owner
响应以下方法:name_for_invoice
和 bill_price_for_invoice
.
目前,我有这个:
需要'test_helper'
class FakeInvoicedItemOwner
attr_accessor :name_for_invoice, :bill_price_for_invoice
end
FactoryGirl.define do
factory 'FakeInvoicedItemOwner' do
name_for_invoice { 'Fake Name' }
bill_price_for_invoice { 12.0 }
end
factory 'Invoicing::InvoicedItem' do
association :invoice, factory: 'Invoicing::Invoice'
owner { FactoryGirl.build('FakeInvoicedItemOwner') }
name { 'FG name' }
billed_price { 1.0 }
end
end
我总是遇到与持久性相关的错误:undefined method 'primary_key' for FakeInvoicedItemOwner:Class
因为 FactoryGirl 试图保留这个 FakeInvoicedItemOwner
实例,但我试图避免这种情况。有没有办法告诉 FactoryGirl 使用假对象而不是提供真实的模型工厂?
编辑:解决方案
(命名错误,Plus::SpentItemStub
应该是Invoicing::Stubs::SpentItem
)
# class inheriting from an invoice-able item
# redefines the methods used for Invoicing
class Plus::SpentItemStub < Plus::SpentItem
def name_for_invoice
'fake name for invoicing'
end
end
FactoryGirl.define do
# factory for my fake model above
factory 'Plus::SpentItemStub' do
end
factory 'Invoicing::InvoicedItem' do
# relation owner using a stubbed instance of my fake model
owner { FactoryGirl.build_stubbed('Plus::SpentItemStub') }
# ...
end
end
您仍然可以为每个关系使用真实模型,但使用 build_stubbed
而不是 build
,后者应该更快更轻:
https://robots.thoughtbot.com/use-factory-girls-build-stubbed-for-a-faster-test
Rails 3.2.13, FactoryGirl, minitest-spec-rails.
我想为 InvoicedItem
模型定义一个工厂。这个InvoicedItem
属于一个(多态的):owner
。这个 owner
可以是 SpentItem
。为了创建 SpentItem
,我需要创建其他几条记录(PricingGroup
、PriceRatio
、Supplier
等)。这很快就变成了一场噩梦。
有没有办法在不使用现有模型的情况下在 FactoryGirl 中定义关联?
基本上,我不想实例化几个 SpentItem
相关模型来测试 InvoicedItem
。我只需要 InvoicedItem 的 owner
响应以下方法:name_for_invoice
和 bill_price_for_invoice
.
目前,我有这个:
需要'test_helper'
class FakeInvoicedItemOwner
attr_accessor :name_for_invoice, :bill_price_for_invoice
end
FactoryGirl.define do
factory 'FakeInvoicedItemOwner' do
name_for_invoice { 'Fake Name' }
bill_price_for_invoice { 12.0 }
end
factory 'Invoicing::InvoicedItem' do
association :invoice, factory: 'Invoicing::Invoice'
owner { FactoryGirl.build('FakeInvoicedItemOwner') }
name { 'FG name' }
billed_price { 1.0 }
end
end
我总是遇到与持久性相关的错误:undefined method 'primary_key' for FakeInvoicedItemOwner:Class
因为 FactoryGirl 试图保留这个 FakeInvoicedItemOwner
实例,但我试图避免这种情况。有没有办法告诉 FactoryGirl 使用假对象而不是提供真实的模型工厂?
编辑:解决方案
(命名错误,Plus::SpentItemStub
应该是Invoicing::Stubs::SpentItem
)
# class inheriting from an invoice-able item
# redefines the methods used for Invoicing
class Plus::SpentItemStub < Plus::SpentItem
def name_for_invoice
'fake name for invoicing'
end
end
FactoryGirl.define do
# factory for my fake model above
factory 'Plus::SpentItemStub' do
end
factory 'Invoicing::InvoicedItem' do
# relation owner using a stubbed instance of my fake model
owner { FactoryGirl.build_stubbed('Plus::SpentItemStub') }
# ...
end
end
您仍然可以为每个关系使用真实模型,但使用 build_stubbed
而不是 build
,后者应该更快更轻:
https://robots.thoughtbot.com/use-factory-girls-build-stubbed-for-a-faster-test