Unable to Mock when using Entity Functions

4 posts, 0 answers
  1. John
    John avatar
    3 posts
    Member since:
    Mar 2011

    Posted 06 Jan 2012 Link to this post

    I have been attempting to learn how we can mock out calls to entity framework in our project. I ran into this problem where we are using EntityFunctions.TruncateTime. The error says: this function can only be invoked from linq to entities. If this should be supported help on what i'm doing wrong, if its not then i guess we are out of luck for those scenarios.

    from audit in _context.Audits
    where audit.DTS < EntityFunctions.TruncateTime(mydate)
    select audit

    var fakeEntities = Mock.Create<SomeDBEntities>();
    var audits = GetAudits();
    Mock.Arrange(() => fakeEntities.Audits).ReturnsCollection(audits);
    var target = new ReportRepository(fakeEntities);
    var result = target.test(DateTime.Now);
    Assert.AreEqual(2, result.Count());

  2. Ricky
    Ricky avatar
    467 posts

    Posted 11 Jan 2012 Link to this post

    Hi John,

    Thanks again for reporting the problem. It could be the issue that
    EntityFunctions.TruncateTime could not be used in this context but will investigate more on that and therefore creating an PITS entry.

    You can further check the progress of the task here:

    Kind Regards,
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft R3 2016 release webinar banner
  4. Motheo
    Motheo avatar
    1 posts
    Member since:
    Sep 2015

    Posted 16 Nov 2015 in reply to Ricky Link to this post

    Hi Guys,


    Didi you get solution to the above problem.

  5. Stefan
    Stefan avatar
    198 posts

    Posted 17 Nov 2015 Link to this post

    Hi Motheo,

    The EntityFunctions.TruncateTime method doesn't actually do anything - it just gets translated into the equivalent DB function when the LINQ query is translated to SQL.

    You can arrange the function to implement the semantical behavior of that DB call.
    Mock.Arrange(() => EntityFunctions.TruncateTime(Arg.IsAny<DateTime?>())).Returns((DateTime? dt) => dt != null ? dt.Value.Date : null);

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top