你会如何伪造以下内容:
public interface IBlah
{
Func<T, bool> ApplyFilter<T>(Func<T, bool> predicate) where T:IMessage;
}
Run Code Online (Sandbox Code Playgroud)
我想要的是假的简单地返回它的论点而不做任何改变.但是,我想验证假冒被称为一次.用例如下:
public class Something
{
public Something(IBlah blah) { _blah = blah; }
public bool DoSomething(SomeValue m, Func<SomeValue, bool> predicate)
{
Func<SomeValue, bool> handler = _blah.ApplyFilter(predicate);
return handler(m);
}
}
Run Code Online (Sandbox Code Playgroud)
即假冒需要作为通过,但我也需要能够验证它已被使用.
最好的方法是什么?
[请不要担心这个人为的例子......有很多事情在幕后进行,但我已将其简化为上面的例子.]
这能解决您的问题吗?它将通过谓词并验证ApplyFilter是否被调用了一次
[Fact]
public void TestFeature()
{
var fake = A.Fake<IBlah>();
A.CallTo(() => fake.ApplyFilter(A<Func<int, bool>>.Ignored)).ReturnsLazily(x =>
{
return x.GetArgument<Func<int, bool>>("predicate");
});
var something = new Something(fake);
var result = something.DoSomething(1, x => x > 1);
Assert.False(result);
A.CallTo(() => fake.ApplyFilter(A<Func<int, bool>>.Ignored)).MustHaveHappened(Repeated.Exactly.Once);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1788 次 |
最近记录: |