title: How do I use Moq and DbFunctions in unit tests to prevent a NotSupportedException?
body: <p>I'm currently attempting to run some unit tests on a query that is running through the Entity Framework. The query itself runs without any issues on the live version, but the unit tests are always failing.</p>
  
  <p>I've narrowed this down to my usage of DbFunctions.TruncateTime, but I don't know of a way around this to get the unit tests to reflect what is happening on the live server.</p>
  
  <p>Here is the method that I am using:</p>
  
  <pre><code>    public System.Data.DataTable GetLinkedUsers(int parentUserId)
      {
          var today = DateTime.Now.Date;
  
          var query = from up in DB.par_UserPlacement
                      where up.MentorId == mentorUserId
                          &amp;&amp; DbFunctions.TruncateTime(today) &gt;= DbFunctions.TruncateTime(up.StartDate)
                          &amp;&amp; DbFunctions.TruncateTime(today) &lt;= DbFunctions.TruncateTime(up.EndDate)
                      select new
                      {
                          up.UserPlacementId,
                          up.Users.UserId,
                          up.Users.FirstName,
                          up.Users.LastName,
                          up.Placements.PlacementId,
                          up.Placements.PlacementName,
                          up.StartDate,
                          up.EndDate,
                      };
  
          query = query.OrderBy(up =&gt; up.EndDate);
  
          return this.RunQueryToDataTable(query);
      }
  </code></pre>
  
  <p>If I comment out the lines with DbFunctions in, the tests all pass (except for the ones that are checking that only valid results for a given date are run).</p>
  
  <p>Is there a way I can provide a mocked version of DbFunctions.TruncateTime to use in these tests? Essentially it should just be returning Datetime.Date, but that isn't available in EF queries.</p>
  
  <p><strong>Edit:</strong> Here's the test that's failing that uses the date check:</p>
  
  <pre><code>    [TestMethod]
      public void CanOnlyGetCurrentLinkedUsers()
      {
          var up = new List&lt;par_UserPlacement&gt;
          {
              this.UserPlacementFactory(1, 2, 1), // Create a user placement that is current
              this.UserPlacementFactory(1, 3, 2, false) // Create a user placement that is not current
          }.AsQueryable();
  
          var set = DLTestHelper.GetMockSet&lt;par_UserPlacement&gt;(up);
  
          var context = DLTestHelper.Context;
          context.Setup(c =&gt; c.par_UserPlacement).Returns(set.Object);
  
          var getter = DLTestHelper.New&lt;LinqUserGetLinkedUsersForParentUser&gt;(context.Object);
  
          var output = getter.GetLinkedUsers(1);
  
          var users = new List&lt;User&gt;();
          output.ProcessDataTable((DataRow row) =&gt; students.Add(new UserStudent(row)));
  
          Assert.AreEqual(1, users.Count);
          Assert.AreEqual(2, users[0].UserId);
      }
  </code></pre>
  
  <p><strong>Edit 2:</strong> This is the message and debug trace from the test in question:</p>
  
  <pre><code>Test Result: Failed
  
  Message: Assert.AreEqual failed. Expected:&lt;1&gt;. Actual:&lt;0&gt;
  
  Debug Trace: This function can only be invoked from LINQ to Entities
  </code></pre>
  
  <p>From what I've read, this is because there isn't a LINQ to Entities implementation of this method that could be used in this place for the Unit Test, although there is on the live version (as it's querying an SQL server).</p>