Moq:为方法引发,返回Task

时间:2017-03-09 13:32:09

标签: c# moq

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

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

测试代码未编译(我希望在调用FooAsync之后引发事件):

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

如何解决这个问题?

1 个答案:

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