title: C# Moq - Mocking Action<T> parameter
body: <p>I've been strugling with a moq setup for a while, and I'd like get some help from you, any help is appreciated.</p>
  
  <h2>Basic codes</h2>
  
  <pre><code>public interface IMessageHub
  {
      void Subscribe&lt;T&gt;(string topic, Action&lt;T&gt; callback);
  }
  </code></pre>
  
  <p>Having message hub, where you can subscribe to topics, and if there is a message that was published, it calls the callback with the message.</p>
  
  <p>Every message is derived from</p>
  
  <pre><code>public class MessageBase { }
  </code></pre>
  
  <p>And for the example, I have created 2 message types:</p>
  
  <pre><code>public class RefreshMessage : MessageBase { }
  
  public class CancelMessage : MessageBase { }
  </code></pre>
  
  <p>In some part of the code, there is 2 subscribtion for the messages</p>
  
  <pre><code>...
  MessageHub.Subscribe&lt;RefreshMessage&gt;("Refresh", RefreshMethod);
  MessageHub.Subscribe&lt;CancelMessage&gt;("Cancel", CancelMethod);
  ...
  </code></pre>
  
  <p>The callback methods are private, so I can only get them through setup in the unit tests.</p>
  
  <pre><code>private void RefreshMethod(RefreshMessage message) { ... }
  private void CancelMethod(CancelMessage message) { ... }
  </code></pre>
  
  <p>These parts are provided by a framework, so I can't change them.</p>
  
  <h2>Unit tests</h2>
  
  <p>In the unit tests I want to mock the MessageHub with a Moq , and mock the Subscribe method for every action, and get the callback method that the subscribe method was called with.</p>
  
  <p>Normally, I would have to create 2 setups for every messagetype like this:</p>
  
  <pre><code>var messageHubMock = new Mock&lt;IMessageHub&gt;();
  var methods = new List&lt;dynamic&gt;();
  
  //setup for RefreshMessage
  messageHubMock.Setup(_=&gt;_.Subscribe(It.IsAny&lt;string&gt;,It.IsAny&lt;Action&lt;RefreshMessage&gt;&gt;())
  .Callback&lt;string,Action&lt;RefreshMessage&gt;&gt;((s,a)=&gt; methods.Add(a));
  
  //setup for CancelMessage
  messageHubMock.Setup(_=&gt;_.Subscribe(It.IsAny&lt;string&gt;,It.IsAny&lt;Action&lt;CancelMessage&gt;&gt;())
  .Callback&lt;string,Action&lt;CancelMessage&gt;&gt;((s,a)=&gt; methods.Add(a));
  </code></pre>
  
  <h2>Setup question</h2>
  
  <p>Question is, how can I do the 2 setup in 1 setup?</p>
  
  <p>The following setups ARE NOT WORKING, but showing that those I've tried.</p>
  
  <pre><code>messageHubMock.Setup(_=&gt;_.Subscribe(It.IsAny&lt;string&gt;,It.IsAny&lt;Action&lt;object&gt;&gt;())
  .Callback&lt;string,Action&lt;object&gt;&gt;((s,a)=&gt; methods.Add(a));
  
  messageHubMock.Setup(_=&gt;_.Subscribe(It.IsAny&lt;string&gt;,It.IsAny&lt;Action&lt;dynamic&gt;&gt;())
  .Callback&lt;string,Action&lt;dynamic&gt;&gt;((s,a)=&gt; methods.Add(a));
  
  messageHubMock.Setup(_=&gt;_.Subscribe(It.IsAny&lt;string&gt;,It.IsAny&lt;object&gt;())
  .Callback&lt;string,object&gt;((s,a)=&gt; methods.Add(a));
  //does not compile
  messageHubMock.Setup(_=&gt;_.Subscribe(It.IsAny&lt;string&gt;,It.IsAny&lt;Action&gt;())
  .Callback&lt;string,Action&gt;((s,a)=&gt; methods.Add(a));
  
  messageHubMock.Setup(_=&gt;_.Subscribe(It.IsAny&lt;string&gt;,It.IsAny&lt;Action&lt;MessageBase&gt;&gt;())
  .Callback&lt;string,Action&lt;MessageBase&gt;&gt;((s,a)=&gt; methods.Add(a));
  </code></pre>
  
  <p>Every of these does not fit for the expression, so even though the subscibre was called in the code, Moq does not run the Callback on the setup, because the subscribe was called with "different" arguments. </p>
  
  <p>I suppose I need to create my own expression filter logic on this.
  Can you help me out, how to create this setup, with 1 setup only?</p>