C# 生成子列表
我有以下两门课:C# 生成子列表,c#,.net,integration-testing,xunit,autofixture,C#,.net,Integration Testing,Xunit,Autofixture,我有以下两门课: 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 c
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; }
}
公共类博客
{
公共博客()
{
Posts=newhashset();
}
public int BlogId{get;set;}
公共字符串名称{get;set;}
公共ICollection Posts{get;private set;}
}
公营职位
{
公共int PostId{get;set;}
public int BlogId{get;set;}
公共字符串Uri{get;set;}
公共博客Blog{get;set;}
}
我尝试使用AutoFixture生成我想在测试中使用的样本数据
var blogs = new List<Blog>(new Fixture().Build<Blog>()
.Without(x => x.BlogId)
.CreateMany(10));
var blogs=newlist(newfixture().Build())
.Without(x=>x.BlogId)
.1(10));
但是帖子的集合是空的
问题是如何使用Autofixture生成博客和相应的帖子,比如说每10个博客10篇帖子
但是帖子的集合是空的
不完全是,;Posts
集合为空,因为它们在Blog
构造函数中被初始化为空哈希集
如何使用Autofixture生成博客和相应的帖子
在Do
块中使用fixture.AddManyTo
:
var fixture = new Fixture();
var blogs = fixture.Build<Blog>()
.Without(b => b.BlogId)
.Do(b => fixture.AddManyTo(b.Posts, 10))
.CreateMany(10);
var fixture=newfixture();
var blogs=fixture.Build()
.Without(b=>b.BlogId)
.Do(b=>fixture.AddManyTo(b.Posts,10))
.1(10);
这将创建10个
Blog
对象,每个对象在Posts
集合中包含10个Post
对象。因为Posts
属性具有私有setter。AutoFixture不具有为非公共字段或属性指定值的内置支持。这是故意的。