Aki*_*lov 5 unit-testing mocking fakeiteasy
我有一个复杂的对象,我试图伪造.
interface IContext
{
User User { get; }
}
A.CallTo(
() => _fakeContext.User.Subscription.Attributes)
.Returns(new List<Attribute>());
Run Code Online (Sandbox Code Playgroud)
但我得到了下一个例外:
The current proxy generator can not intercept the specified method for the following reasons: - Non virtual methods can not be intercepted
所有嵌套类型都是属性,它们是具有get; set;属性修饰符的简单贫血类型.当我调查调试器时,他们都是假的.
有没有办法设置链的最后一个属性,避免设置以前的所有属性?
如果你的对象足够贫乏,你可能想尝试一下AutoFixture:
var fake = A.Fake<>();
var fixture = new Fixture();
// If it's possible [1], AutoFixture will generate entire object graph
var user = fixture.CreateAnonymous<User>();
// Since data will be random [2], you can overwrite properties as you like
user.User.Subscription.Attributes = new List<Attributes>();
A.CallTo(() => fake.User).Returns(user);
Run Code Online (Sandbox Code Playgroud)
.Build方法提供流畅的API来定制对象自动生成,因此可以控制随机性| 归档时间: |
|
| 查看次数: |
1479 次 |
| 最近记录: |