【发布时间】:2019-03-07 23:47:04
【问题描述】:
我想我遗漏了一些东西,但我想做的是:
我的 C# 代码中有两个数据库实体。一个是另一个的孩子,因此孩子包含一个应该引用父母 ID 的字段。
父类如下
public class Product
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public decimal DeliveryPrice { get; set; }
}
子类如下:
public class ProductOption
{
public Guid Id { get; set; }
public Guid ProductId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
我已经创建了一个随机“父母”列表:
var products = fixture.CreateMany<Product>(5).ToList();
然后我想做的是创建 10 个子对象,并从 AutoFixture 创建的产品列表中随机给它们一个ProductId。所以我尝试了这个:
var rand = new Random();
var options = fixture.Build<ProductOption>()
.With(option => option.ProductId, products[rand.Next(0, 5)].Id)
.CreateMany(10)
.ToList();
它几乎起作用了,但我发现所有的ProductIds 都是同一个,所以它显然只命中了一次rand.Next。
我正在做的事情是否可行/可取?
【问题讨论】:
标签: c# .net unit-testing testing autofixture