title: C# Moq Unit Testing with Lambda Expression or Func delegate
body: <p>I am using Moq with C# for unit testing.</p>
  
  <p>I have the following implementation to test:</p>
  
  <pre><code>var jobsSelectionToMapJobModel = new List&lt;Job&gt;();
  var jobsSelectionToMapDataTableModel =
                  _enumerableWrapperService.Select(jobsSelectionToMapJobModel,
                      x =&gt; _convertJobToJobsModelForDataTableService.Convert(umbracoHelper, x));
  </code></pre>
  
  <p>The Select Method in <code>_enumerableWrapperService.Select()</code> has the following signature, like the Linq one:</p>
  
  <pre><code>Enumerable&lt;TResult&gt; Select&lt;TSource, TResult&gt;(IEnumerable&lt;TSource&gt; source,
              Func&lt;TSource, TResult&gt; selector);
  </code></pre>
  
  <p>I would like to test out that the selector Func delegate’s content has the correct logic set in it.  The umbracoHelper variable is an external paramater passed on to this method.</p>
  
  <p>If you use the <code>It.IsAny&lt;Func&lt;TSource, TResult&gt;&gt;()</code>, we will not be asserting that the logic is fine.  On the other hand, I was thinking of using something like <code>It.Is&lt;Func&lt;TSource,TResult&gt;(func =&gt; XXXXXXXX)</code>, but cannot figure out how to make it work</p>
  
  <p>See below examples:</p>
  
  <p>UNIT TEST EXAMPLE 1</p>
  
  <pre><code>_listToReturn= new List&lt;JobsModelForDataTable&gt;();
  _listOfJobs= new List&lt;Job&gt;();
  GetMockFor&lt;IEnumerableWrapperService&gt;()
                  .Setup(x =&gt; x.Select(_listOfJobs,
                       It.IsAny&lt;Func&lt;Job, JobsModelForDataTable&gt;&gt;()))
                  .Returns(_listToReturn);
  </code></pre>
  
  <p>//The problem with the above is that you are not asserting the correct logic as it accepts any Func which has that same signature</p>
  
  <p>UNIT TEST EXAMPLE 2</p>
  
  <pre><code> _listToReturn= new List&lt;JobsModelForDataTable&gt;();
  _listOfJobs= new List&lt;Job&gt;();
  GetMockFor&lt;IEnumerableWrapperService&gt;()
                  .Setup(x =&gt; x.Select(_listOfJobs,
                       It.Is&lt;Func&lt;Job, JobsModelForDataTable&gt;&gt;(f =&gt; ......)))
                  .Returns(_listToReturn);
  </code></pre>
  
  <p>//This might be an option but I cannot figure out how to make it work</p>
  
  <p>Or maybe we need to rethink what we are doing in another way?</p>
  
  <p>I would appreciate any help on this, on how I can test it out.</p>
  
  <hr>
  
  <p>Thank you in advance for your help.</p>