将Foo
的返回类型从void
更改为Task
后:
interface A {
event EventHandler<EventArgs> Created;
Task FooAsync();
}
测试代码未编译(我希望在调用FooAsync
之后引发事件):
aMock.Setup(x=>x.FooAsync()).Raises(x => x.Created+= null, EventArgs.Empty);
如何解决这个问题?
答案 0 :(得分:2)
这对我有用。
[TestMethod]
public async Task Test()
{
var task = Task.CompletedTask;
var a = new Mock<A>();
a.Setup(x => x.FooAsync()).Returns(task).Raises(x => x.Created += null, EventArgs.Empty);
a.Object.Created += (s, e) =>
{
};
await a.Object.FooAsync();
}