开发者

AutoFixture - how do I call a method, how to set a private setter of an autoproperty?

Here's my class:

public MyClass {
  public int Id { get; private set; }
  public SetAssignableId (int id) {
    this.Id = id;
  }
}

I would like to have AutoFixture set the Id via S开发者_StackOverflow社区etAssignableId or the private setter.


There's no way to invoke a private setter with AutoFixture today. Although it uses reflection internally, by design it respects a type's public API.

There's an outstanding request on the Issue Board to make this more customizable - if you read the work item closely you will see that there's a request to enable filling of protected setters.

However, with the example given, it's certainly possible to invoke the SetAssignableId method. A Customization like this should do the trick:

fixture.Customize<MyClass>(c => 
    c.Do(mc => 
        mc.SetAssignableId(fixture.CreateAnonymous<int>())));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜