开发者

NBuilder - Issue Creating Nested Object

开发者 https://www.devze.com 2023-03-02 01:41 出处:网络
I\'m trying to create some dummy objects which have a hierachy like this: Post ----User Pret开发者_StackOverflow中文版ty simple.

I'm trying to create some dummy objects which have a hierachy like this:

Post
 |
 ----User

Pret开发者_StackOverflow中文版ty simple.

Here's the code:

var user = Builder<User>.CreateNew().Build();
var posts  = Builder<Post>.CreateListOfSize(100)
                .All()
                .With(x => x.User == user)
                .Build();

But for each item, post.User is null.

Any ideas?


should it be?

.With(x => x.User = user)


I wanted to do similar but couldn't find a way of generating user objects with different values (accepted answer would have same data for all user child objects). I ended up using AutoFixture to do this instead:

var fixture = new Fixture {RepeatCount = 100};
var posts = fixture.Repeat(fixture.Create<Post>);
0

精彩评论

暂无评论...
验证码 换一张
取 消