问题描述
下面是我的类:
public MyClass {
public int Id { get; private set; }
public SetAssignableId (int id) {
this.Id = id;
}
}
我想有AutoFixture通过SetAssignableId设置ID或私人的制定者。
I would like to have AutoFixture set the Id via SetAssignableId or the private setter.
推荐答案
有没有办法今天援引私人二传手与AutoFixture。虽然使用反射内部,设计它尊重一个类型的公共API。
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.
有是一个的未完成的请求,使这个更具个性 - 如果你读的工作项观察你会看到有使受保护的setter方法填充请求
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.
然而,随着给定的例子中,当然有可能以调用SetAssignableId方法。像这样的定制应该做的伎俩:
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>())));
这篇关于AutoFixture - 我怎么调用一个方法,如何设置autoproperty的私人二传手?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!