使用异步操作的最小起订量测试不起作用

moq testing with an asynchronous operation does not work

我正在尝试使用异步方法进行模拟测试,但我不知道如何设置最小起订量测试以允许它。

noSQLProvider.Setup(x => x.CreateDocumentAsync(It.IsIn<Uri>(), It.IsAny<object>())).Returns();

错误告诉我:

Usage: ResourceResponse x = await CreatDocumentAsync(...);

an expression tree may not contain a call or invocation that uses optional arguments.

我猜你正在使用 this?

在这种情况下,错误消息非常清楚地告诉您该方法具有可选参数,您需要明确说明它们:

noSQLProvider.Setup(x => x.CreateDocumentAsync(It.IsIn<Uri>(),
                                               It.IsAny<object>(),
                                               It.IsAny<RequestOptions>(),
                                               It.IsAny<bool>())).Returns();

这是与异步无关的限制。

NB - 我不确定 It.IsAny 比较对 null 的表现如何,因此您可能必须将 It.IsAny<RequestOptions>() 替换为明确将该参数设置为 null 的内容(这是默认)。