简体   繁体   English

在回调模拟设置上设置 ManualResetEvent 时出错

[英]Error while setting ManualResetEvent on callback mock setup

I am making use of ManualResetEvent class in a test.我在测试中使用ManualResetEvent类。

Basically, I want to invoke the Set() method when a particular function is called.基本上,我想在调用特定函数时调用Set()方法。 This looks like:这看起来像:

var mre = new ManualResetEvent(false);
mockObj.Setup(dmc => dmc.Foo(param1, param2, param3)).Callback(mre.Set()); //Error here.

However, I get an error saying:但是,我收到一条错误消息:

Cannot convert from bool to 'System.Action'无法从 bool 转换为“System.Action”

when I try to set the mre .当我尝试设置mre

Am I doing anything wrong here?我在这里做错了什么吗?

The error message says it all错误信息说明了一切

Cannot convert from bool to 'System.Action'无法从 bool 转换为“System.Action”

Callback requires a lambda expression / Action Callback需要一个 lambda 表达式/动作

//...
var mre = new ManualResetEvent(false);
mockObj
    .Setup(dmc => dmc.Foo(param1, param2, param3))
    .Callback(() => mre.Set()); //<-- Callback requires an Action
//...

Reference Moq Quickstart to get a better under standing of how to use the mocking framework.参考Moq Quickstart以更好地了解如何使用模拟框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM