title: How do you shim FirstOrDefault in EntityFramework?
body: <p>Using Microsoft Fakes and Entityframework, I am trying to create a shim for FirstOrDefault().  I have a method I would like to unit test that looks like this:</p>
  
  <pre><code>protected override v_Titles GetEntityByKey(Guid key)
      {
          var result = Context.v_Titles.FirstOrDefault(t =&gt; t.Id == key);
          if ( result == null )
          {
              throw new HttpResponseException(HttpStatusCode.NotFound);
          }
          return result;
      }
  </code></pre>
  
  <p>Which is called by a public method Get(Guid key).</p>
  
  <p>I set up fakes and shimed the database and the v_Titles collection:</p>
  
  <pre><code>public void SetUp()
      {
          _titles = new List&lt;v_Titles&gt;
          {
              new v_Titles
              {
                  Id = new Guid(1,0,0,0,0,0,0,0,0,0,0),
                  PostedDate = new DateTime(2013,1,1),
                  Title = "2013-01-01"
              },
              new v_Titles
              {
                  Id = new Guid(2,0,0,0,0,0,0,0,0,0,0),
                  PostedDate = new DateTime(2013,5,23),
                  Title = "2013-05-23"
              },
              new v_Titles
              {
                  Id = new Guid(3,0,0,0,0,0,0,0,0,0,0),
                  PostedDate = new DateTime(2013,8,10),
                  Title = "2013-08-10"
              }
          };
  
  
          _shimTitles = new ShimDbSet&lt;v_Titles&gt;();
          _shimTitles.Bind(_titles);
  
          ...
  
          _databaseShim = new shimDatabaseEntities();
          _databaseShim.v_TitlesGet = () =&gt; _shimTitles.Instance;
  
          _target = new TitlesController
          {
              Context = _databaseShim.Instance
          };
  </code></pre>
  
  <p>Further where the ... is I've tried the following shims to get FirstOrDefault to work:</p>
  
  <pre><code>            System.Linq.Fakes.ShimEnumerableQuery&lt;v_Titles&gt;.AllInstances.GetEnumerator = (a) =&gt;
              { return _titles.GetEnumerator(); };
          System.Linq.Fakes.ShimQueryable.FirstOrDefaultOf1IQueryableOfM0&lt;v_Titles&gt;((a) =&gt;
              {
                  return _titles.FirstOrDefault();
              });
          databaseEntities.AllInstances.v_TitlesGet = (a) =&gt; _shimTitles.Instance;
  </code></pre>
  
  <p>My unit test looks like this:</p>
  
  <pre><code>        [TestMethod]
      public void Get_ReturnsOneItem_Test()
      {
          using (ShimsContext.Create())
          {
              var expected = new Guid(2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
              SetUp();
              var result = _target.Get(expected);
          }
      }
  </code></pre>
  
  <p>When running the test I get the following exception on the .FirstOrDefault() call.</p>
  
  <blockquote>
    <p>System.ArgumentNullException was unhandled by user code
      HResult=-2147467261
      Message=Value cannot be null.
    Parameter name: arguments
      Source=System.Core
      ParamName=arguments
      StackTrace:
           at System.Linq.Expressions.Expression.RequiresCanRead(Expression expression, String paramName)
           at System.Linq.Expressions.Expression.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arg, ParameterInfo pi)
           at System.Linq.Expressions.Expression.ValidateArgumentTypes(MethodBase method, ExpressionType nodeKind, ReadOnlyCollection<code>1&amp; arguments)
           at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable</code>1 arguments)
           at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, Expression[] arguments)
           at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable<code>1 source, Expression</code>1 predicate)
           at WebSite.Web.Controllers.Api.TitlesController.GetEntityByKey(Guid key) in Controllers\Api\TitlesController.cs:line 42
           at System.Web.Http.OData.EntitySetController`2.Get(TKey key)
           at WebSite.Web.Tests.TitlesController_Tests.Get_ReturnsOneItem_Test() in TitlesController_Tests.cs:line 96
      InnerException: </p>
  </blockquote>
  
  <p>How do I get the FirstOrDefault return something?  It does not even have to execute the query, all I want to test is that I am calling FirstOrDefault and returning the value it retuns or throws the exception when it returns null.</p>