Autofixture 配置父子关系 [英] Autofixture configure relationship between parent and child
问题描述
似乎我找不到一种简单的方法来使用 autofixture 在父子之间建立关系.假设我有一个类 Order
和 OrderLine
并且 OrderLine
通过 OrderId<与
Order
链接/代码>.现在我有一个 Order
列表,我希望列表中的每个订单都有几个 OrderLine
并且 OrderLine
应该具有相同的 OrderId
根据 Order
类.
It seems like I can't find an easy way to create relationship between parent and child using autofixture. Let is say I have a class Order
and OrderLine
and OrderLine
is linked with Order
by OrderId
. Now I have a list of Order
s and I want that each order inside the list have a few OrderLine
and OrderLine
should have the same OrderId
as per the Order
class.
如何配置这种关系?fixture.AddManyTo(orderlist)
添加许多带有许多 OrderLine
的订单,但它们都是随机的 Id
和 OrderIds
.
How to configure this relationship? fixture.AddManyTo(orderlist)
adds many orders with many OrderLine
s but they are all random Id
s and OrderIds
.
fixture.Create
或 Customize
似乎不是正确的??
fixture.Create
or Customize
doesn't seem to be the right ones??
有没有什么简单的方法可以做到这一点?
Is there any easy way to do this?
推荐答案
我遇到了这个完全相同的问题,有一个 Nuget 包,它为你扩展了fixture.本质上,它确保 OrderLine.Order.Id == OrderLine.OrderId.
I had come across this exact same issue, there is a Nuget package which extends fixture for you. In essence it makes sure that OrderLine.Order.Id == OrderLine.OrderId.
这篇关于Autofixture 配置父子关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!