Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在模拟中为动作委托赋值_C#_Unit Testing_Moq - Fatal编程技术网

C# 如何在模拟中为动作委托赋值

C# 如何在模拟中为动作委托赋值,c#,unit-testing,moq,C#,Unit Testing,Moq,我想将true赋值给带有moq的操作参数 void SaveAllCustomers( List<Customer> Customers, Action<bool, string> result); void SaveAllCustomers( 列出客户名单, 行动结果); 因此,我可以根据该操作的结果验证覆盖率。我怎样才能做到这一点? 谢谢您可以在设置模拟时使用回调 mock .Setup(_ => _.SaveAllC

我想将true赋值给带有moq的
操作
参数

void SaveAllCustomers(
        List<Customer> Customers,
        Action<bool, string> result);
void SaveAllCustomers(
列出客户名单,
行动结果);
因此,我可以根据该操作的
结果
验证覆盖率。我怎样才能做到这一点?
谢谢

您可以在设置模拟时使用回调

mock
    .Setup(_ => _.SaveAllCustomers(It.IsAny<List<Customer>>(), It.IsAny<Action<bool, string>>()))
    .Callback((List<Customer> customers, Action<bool, string> result) => {
        if(result != null)
            result(true, "");            
    });
mock
.Setup(=>u.SaveAllCustomers(It.IsAny(),It.IsAny()))
.Callback((列出客户,操作结果)=>{
如果(结果!=null)
结果(真“);
});

我遇到了这个错误:对象引用未设置为对象的实例确定您需要提供更多上下文。演示测试以及如何使用模拟。用这些细节更新原始问题。这很奇怪,测试中的方法是异步的,所以当我从“act”部分删除task async并等待时,它就工作了