Search code examples
c#unit-testingmoqmanualresetevent

Error while setting ManualResetEvent on callback mock setup


I am making use of ManualResetEvent class in a test.

Basically, I want to invoke the Set() method when a particular function is called. 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'

when I try to set the mre.

Am I doing anything wrong here?


Solution

  • The error message says it all

    Cannot convert from bool to 'System.Action'

    Callback requires a lambda expression / Action

    //...
    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.