This is a migrated thread and some comments may be shown as answers.

Unable to Mock when using Entity Functions

3 Answers 413 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 2
John asked on 06 Jan 2012, 05:26 PM
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());
Thanks
John

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 11 Jan 2012, 09:41 PM
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:
http://www.telerik.com/support/pits.aspx#/public/justmock/9290


Kind Regards,
Mehfuz
the Telerik team

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

0
Motheo
Top achievements
Rank 1
answered on 16 Nov 2015, 01:40 PM

Hi Guys,

 

Didi you get solution to the above problem.

0
Stefan
Telerik team
answered on 17 Nov 2015, 08:40 AM
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);


Regards,
Stefan
Telerik
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
Tags
General Discussions
Asked by
John
Top achievements
Rank 2
Answers by
Ricky
Telerik team
Motheo
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or