title: Getting incorrect number of paramaters in callback error using Moq
body: <p>I'm getting the following error when trying to fake a db set as a list using Moq:</p>
  
  <blockquote>
    <p>System.ArgumentException   HResult=0x80070057   Message=Invalid
    callback. Setup on method with 0 parameter(s) cannot invoke callback
    with different number of parameters (1).   Source=Moq   StackTrace:<br>
    at Moq.MethodCallReturn 2.ValidateReturnDelegate(Delegate callback)<br>
    at Moq.MethodCallReturn 2.Returns(Func`1 valueExpression)    at</p>
  </blockquote>
  
  <p>Here's my code.  It should be relatively simple.</p>
  
  <pre><code>var mockContext = new Mock&lt;IContext&gt;();
  var lhList = new List&lt;LogicHistory&gt;();
  mockContext.Setup(x =&gt; x.Set&lt;LogicHistory&gt;()).Returns(lhList.AsDbSet);
  </code></pre>
  
  <p>Here's my extension method.</p>
  
  <pre><code>public static DbSet&lt;T&gt; AsDbSet&lt;T&gt;(this List&lt;T&gt; sourceList) where T : class
  {
      var queryable = sourceList.AsQueryable();
      var mockDbSet = new Mock&lt;DbSet&lt;T&gt;&gt;();
  
      mockDbSet.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.Provider).Returns(queryable.Provider);
      mockDbSet.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.Expression).Returns(queryable.Expression);
      mockDbSet.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(queryable.ElementType);
      mockDbSet.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(queryable.GetEnumerator());
      mockDbSet.Setup(x =&gt; x.Add(It.IsAny&lt;T&gt;())).Callback&lt;T&gt;(sourceList.Add);
      mockDbSet.Setup(x =&gt; x.Remove(It.IsAny&lt;T&gt;())).Returns&lt;T&gt;(x =&gt; { if (sourceList.Remove(x)) return x; else return null; });
  
      return mockDbSet.Object;
  }
  </code></pre>
  
  <p>I used this exact same method a year ago and it worked perfectly.  I'm looking at my old unit tests and they run fine.  Then I try running this, and it doesn't work.  It looks functionally identical to me.  The only differences are the variable names.  The Set method is identical in both context interfaces:</p>
  
  <pre><code>DbSet&lt;TEntity&gt; Set&lt;TEntity&gt;() where TEntity : class;
  </code></pre>
  
  <p>Anyone have any thoughts on what the problem is?</p>