最小起订量:引发方法,返回任务

Moq: Raises for method, returning Task

Foo 的 return 类型从 void 更改为 Task 后:

interface A {
  event EventHandler<EventArgs> Created;
  Task FooAsync();
}

测试代码无法编译(我想在 FooAsync 调用后引发事件):

aMock.Setup(x=>x.FooAsync()).Raises(x => x.Created+= null, EventArgs.Empty);

如何解决这个问题?

这对我有用。

    [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();
    }