【发布时间】:2021-07-31 12:20:00
【问题描述】:
我有一个对话服务被用作:
this.dialogService.ShowConfirm(
async (dialogRes, error) =>
{
if (dialogRes == true)
{
await Action(win);
}
},
"Prompt message");
我试图设置一个回调并模拟它的执行方式:
this.mockDialogService
.Setup(x => x.ShowConfirm(
It.IsAny<Action<bool, Exception>>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<ContentControl>(),
It.IsAny<string>()))
.Callback((Action<bool, Exception> action) =>
action.Invoke(true, new Exception()));
但是不起作用,抛出异常:
System.ArgumentException : 无效回调。设置带参数的方法(
Action<bool, Exception>, string, string, string, ContentControl, string)无法调用带参数的回调(Action<bool, Exception>)。
我被它困住了,因为我不知道如何继续嘲笑它。
【问题讨论】:
-
正如错误信息所说:你在回调中给出的方法没有足够的参数。并不是说即使
ShowConfirm具有参数的默认值,您仍然需要列出该参数。 -
@KlausGütter 那么在这种情况下,
Action<bool,Exception>会发生什么,因为我需要提供一个值对吗? -
@KlausGütter 我希望通过设置 dialogRes 值来实现默认功能,我该怎么做
-
方法签名似乎是
(Action<bool, Exception>, string, string, string, ContentControl, string)(即6个参数),但您的回调只有一个参数(Action<bool, Exception> action)。只需将其他 5 个参数添加到您的回调中。 -
只需将
Callback((Action<bool, Exception> action) =>更改为Callback((Action<bool, Exception> action, string p2, string p3, string p4, ContentControl p5, string p6) =>。
标签: c# unit-testing nunit moq