【发布时间】:2019-01-22 06:44:08
【问题描述】:
我有以下两个类:
public class Blog
{
public Blog()
{
Posts = new HashSet<Post>();
}
public int BlogId { get; set; }
public string Name { get; set; }
public ICollection<Post> Posts { get; private set; }
}
public class Post
{
public int PostId { get; set; }
public int BlogId { get; set; }
public string Uri { get; set; }
public Blog Blog { get; set; }
}
我尝试使用 AutoFixture 生成我想在测试中使用的示例数据。
var blogs = new List<Blog>(new Fixture().Build<Blog>()
.Without(x => x.BlogId)
.CreateMany(10));
但帖子的收藏为空。
问题是我如何使用 Autofixture 来生成博客和相应的帖子,假设每 10 个博客有 10 个帖子。
【问题讨论】:
-
因为
Posts属性有私有设置器。 AutoFixture 没有内置支持将值分配给非公共字段或属性。这是设计使然。
标签: c# .net integration-testing xunit autofixture