我为我的存储库编写单元测试,我需要使用委托测试存储库方法。首先来自存储库界面public interface IRepository<T>{ ... IQueryable<T> All(); ...}这是我要测试的方法。我将其作为IRepository<RentDetails>的扩展方法public static bool AnyIntersectionForRoomWithDatesExists(this IRepository<RentDetails> repository, Room room, DateTime startDate,DateTime terminationDate){var rent = repository.All().Where(r =>(r.BeginningDate <= startDate && r.TerminationDate > terminationDate && r.TerminationDate < terminationDate) ||(r.BeginningDate > startDate) ||(r.TerminationDate >= terminationDate && r.BeginningDate > startDate && r.BeginningDate < terminationDate)); }在单元测试中,我创建了一个IEnumerable<RentDetails>集合IEnumerable<RentDetails> rentDetailsColection = new List<RentDetails>{new RentDetails(TenantTestHelper.ValidTenant,roomMoq, new DateTime(2012, 1, 1),new DateTime(2012, 2, 1)),new RentDetails(TenantTestHelper.ValidTenant,roomMoq, new DateTime(2012, 3, 1),new DateTime(2012, 7, 1)),new RentDetails(TenantTestHelper.ValidTenant,roomMoq, new DateTime(2012, 10, 1),DateTime.MaxValue)};但我有一个问题如何设置存储库最小起订量?我尝试了以下[Test]var reqestStartDate = new DateTime(2012, 4, 1);var requestTerminationDate = reqestStartDate.AddDays(30);...rentDetailsRepositoryMoq.Setup(c => c.All().Where(r =>(r.BeginningDate <= reqestStartDate &&r.TerminationDate > requestTerminationDate &&r.TerminationDate < requestTerminationDate) ||(r.BeginningDate > reqestStartDate) ||(r.TerminationDate >= requestTerminationDate &&r.BeginningDate > reqestStartDate &&r.BeginningDate < requestTerminationDate))).Returns((IQueryable<RentDetails>)rentDetailsColection.Where(r =>(r.BeginningDate <= reqestStartDate &&r.TerminationDate > requestTerminationDate &&r.TerminationDate < requestTerminationDate) ||(r.BeginningDate > reqestStartDate) ||(r.TerminationDate >= requestTerminationDate &&r.BeginningDate > reqestStartDate && r.BeginningDate < requestTerminationDate)));但我有一个例外NotSupportedException  表达式引用的方法不属于模拟对象:c => c.All()。Where(r =>((((r.BeginningDate .requestTerminationDate)&& r.TerminationDate .reqestStartDate)||(((r.TerminationDate> = .requestTerminationDate && r.BeginningDate> .reqestStartDate)&& r.BeginningDate (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 只需Setup IRepository.All()返回一个IQueryable<T>并断言被测单元正在执行其应做的工作。请注意,根据常规定义,您尝试对存储库进行存根,并且将声明状态。关于c# - 使用Moq测试存储库查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9544538/ (adsbygoogle = window.adsbygoogle || []).push({});
10-09 03:34