title: Invoking Func passed as a parameter to a mock using Moq and C#
body: <p>I have a mocked method that looks like this:</p>
  
  <pre><code>
  class NotMineClass {
    T Execute(Func operation) 
    {
      // do something
      return operation();
    }
  }
  </code></pre>
  
  <p>In my code, I do such as:</p>
  
  <pre><code>
  public MyType MyMethod()
  {
    MyType object = new MyType();
    bool success = notMineClassInstance.Execute(() =>
    {
      // some things
      retVal = injectedObject1.method();
      object.attribute = injectedObject2.method();
      // some other things
      return retVal;
    }
    if (success) 
    {
      object.otherAttribute = someValue;
    }
    return object;
  }
  </code></pre>
  
  <p>My case is, I am testing MyMethod with Moq and I want to verify the Func behaviour is as expected. I have some injected objects in its body, that are mocks, and should be verified; it also starts building my return value, so I can't do any assertion unless I invoke the function passed as parameter.</p>
  
  <p>In Java and jUnit + EasyMock , I would capture the parameter passed, like this:</p>
  
  <pre><code>
  public void testMyMethod() {
    // ...
    Capture &lt; Function &lt; void, Boolean &gt; &gt; functionCapture = Captures.last();
    expect(notMineClassInstance.execute(EasyMock.capture(functionCapture)));
    // Expectations for the body of the function
  
    replay();
  
    functionCapture.getValue().apply(null);
  }
  </code></pre>
  
  <p>How do I do the same using C# + Moq?</p>