本文介绍了如何单元测试GetNewValues()其中包含EntityFunctions.AddDays函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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)

                        });
        }
}


This link shows a good example to unit test EntityFunctions, I used that approach to solve one of my unit test difficulty, but don't know how to solve this problem.

解决方案

Rather than call

System.Data.Objects.EntityFunctions.AddDays

directly, I would inject a custom interface, which forwards the call to that method but which can then be mocked for testing purposes.

这篇关于如何单元测试GetNewValues()其中包含EntityFunctions.AddDays函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 11:51