I am coding a MVC 5 internet application, and I have an expression as follows:
public Expression<Func<Account, bool>> IsExpiresDateTimeLessThanMinimumDaysLeftInFreeTrialSubscription(int minimumDaysLeftInSubscriptionForEmail)
{
return Account => System.Data.Entity.DbFunctions.DiffHours(Account.freeTrialEndDate, DateTime.UtcNow) < minimumDaysLeftInSubscriptionForEmail;
}
When retrieving data from the database, the above expression completes correctly. However, when writing a unit test that uses the above expression I am getting the following error:
This function can only be invoked from LINQ to Entities
I gather that this is because the System.Data.Entity.DbFunctions.DiffHours
function converts the expression
into code that only a database system can understand.
Because of the above fact, is it possible to unit test the above expression when using a mock repository that uses a List
rather than a DbSet
? If not, how should I unit test any code that uses the expression
? Is it possible to unit test the expression
?
Thanks in advance.