下面的示例代码在生产中工作正常,但由于EntityFunctions无法进行单元测试.
我的单元测试项目使用InMemoryDatabase而不是真正的SQL数据库.我可以通过使用计算列myValue和newValue在SQL数据库中创建View来轻松解决我的问题.我喜欢找到一种方法来进行单元测试工作,而无需更改我的方法,也无需创建新的SQL视图
public class EcaseReferralCaseRepository : Repository
{
public class myType
{
public DateTime myValue;
public DateTime newValue;
}
public myType GetNewValues()
{
return
(myType)(from o in context.EcaseReferralCases
select new myType
{
// LINQ to Entity
myValue = (DateTime)System.Data.Objects.EntityFunctions.AddDays(o.StartDate, 0),
newValue = (DateTime)System.Data.Objects.EntityFunctions.AddDays(o.StartDate, 30)
// LINQ to Object
//myValue = o.StartDate.AddDays(0),
//newValue = o.StartDate.AddDays(30)
});
}
}
Run Code Online (Sandbox Code Playgroud)
这个链接显示了单元测试EntityFunctions的一个很好的例子,我用这种方法解决了我的单元测试难度之一,但不知道如何解决这个问题.