0

嗨,我正在向我的数据库编写查询以使用左连接和分组方式获取一些记录,因此我得到一些具有空值的对象,这会产生问题

以下是我的查询

var cards = from classCreative in _unitOfWork.ClassCreatives.All
                        join trainer in _unitOfWork.UserCoreDetails.All on classCreative.RegistrationId equals
                            trainer.Id
                        join imageData in
                            (
                                from imag in
                                    (from cci in _unitOfWork.ClassCreativeImages.All
                                     group cci by cci.ClassCreativeId
                                         into images
                                         select new { ClassCreativeId = images.Key, MinImageId = images.Min(arg => arg.Id) })
                                join i in _unitOfWork.ClassCreativeImages.All
                                on imag.MinImageId equals i.Id
                                select new { imag.ClassCreativeId, i.FileName, i.Title }


                            ) on classCreative.Id equals imageData.ClassCreativeId into classCreativeImages
                        from classImage in classCreativeImages.DefaultIfEmpty()
                        join classBatchTicketVenue in
                            (
                        from batchTicket in
                            (from batch in _unitOfWork.Batches.All
                             join ticket in _unitOfWork.Tickets.All on batch.ClassScheduleId equals
                                 ticket.ClassScheduleId
                             where batch.StartDate > DateTime.Now
                             group new { ticket = ticket, batch = batch } by new { batch.ClassCreativeId }
                                 into batchTicketGrouping
                                 select new
                                 {
                                     batchTicketGrouping.Key.ClassCreativeId,
                                     MinVenueId = batchTicketGrouping.Min(arg => arg.batch.VenueId),
                                     MaxNoOfSesssions = batchTicketGrouping.Max(arg => arg.batch.TotalNoOfSessions),
                                     MinNoOfSesssions = batchTicketGrouping.Min(arg => arg.batch.TotalNoOfSessions),
                                     StartDate = batchTicketGrouping.Min(arg => arg.batch.StartDate),
                                     MinPrice = batchTicketGrouping.Min(arg => arg.ticket.Price),
                                     VenueCount = batchTicketGrouping.Select(arg => arg.batch.VenueId).Distinct().Count()
                                 })
                        join venue in _unitOfWork.Venues.All on batchTicket.MinVenueId equals venue.Id
                        select new
                        {
                            batchTicket.MaxNoOfSesssions,
                            batchTicket.MinNoOfSesssions,
                            batchTicket.MinPrice,
                            batchTicket.MinVenueId,
                            batchTicket.StartDate,
                            batchTicket.VenueCount,
                            venue.LocalityId,
                            venue.CityId,
                            batchTicket.ClassCreativeId
                        }
                            ) on classCreative.Id equals classBatchTicketVenue.ClassCreativeId into classCards
                        from batchVenueTicketData in classCards.DefaultIfEmpty()
                        select new
                        ClassCard
                        {
                            Title = classCreative.Title,
                            Firstname = trainer.FirstName,
                            LastName = trainer.LastName,
                            Id = classCreative.Id,
                            TrainerId = trainer.Id,
                            MaxSessionCount = batchVenueTicketData.ClassCreativeId == null ? null : (int?) batchVenueTicketData.MaxNoOfSesssions,
                            MinSessionCount = batchVenueTicketData.ClassCreativeId == null ? null : (int?) batchVenueTicketData.MinNoOfSesssions,
                            MinPrice = batchVenueTicketData.ClassCreativeId == null ? null : (decimal?) batchVenueTicketData.MinPrice,
                            StartDate = batchVenueTicketData.ClassCreativeId == null ? null : (DateTime?) batchVenueTicketData.StartDate,
                            VenueCount = batchVenueTicketData.ClassCreativeId == null ? 0 : (int?) batchVenueTicketData.VenueCount,
                            CityId = batchVenueTicketData.ClassCreativeId == null ? null : (int?) batchVenueTicketData.CityId,
                            LocaltyId = batchVenueTicketData.ClassCreativeId == null ? null : (int?) batchVenueTicketData.LocalityId,
                            FileName = classImage.ClassCreativeId == null ? null : classImage.FileName,
                            ImageAlt = classImage.ClassCreativeId == null ? null : classImage.Title

                        }

此查询工作正常,但其单元测试失败

下面给出的是它的单元测试

 [TestMethod]
        [Isolated]
        public void ClassCardAll_Test_Must_Return_All_ClassCards()
        {
            var searchContext = Isolate.Fake.AllInstances<SearchContext>();
            var loggger = Isolate.Fake.Instance<ILogger>(Members.ReturnRecursiveFakes);
            var searchAccesor = new SearchAccessor(new SearchUnitOfWork(new SearchContext()), new TypeMapperService(),
                                                    loggger, new CacheService());
           Isolate.WhenCalled(() => searchContext.ClassCreatives)
                   .WillReturn(new FakeClassCreativeSearchDbSet(GetClassCreativeSearchCollection()));
            Isolate.WhenCalled(() => searchContext.ClassCreativeImageSearches)
                   .WillReturn(new FakeClassCreativeImageSearchDbSet(GetClassCreativeImageSearchCollection()));
            Isolate.WhenCalled(() => searchContext.Batches)
                   .WillReturn(new FakeBatchSearchDbSet(GetBatchSearchCollection()));
            Isolate.WhenCalled(() => searchContext.Venues)
                   .WillReturn(new FakeVenueSearchDbSet(GetVenueSearchCollection()));
            Isolate.WhenCalled(() => searchContext.Users)
                   .WillReturn(new FakeUserCoreDetailDbSet(GetUserCoreDetailCollection()));
            Isolate.WhenCalled(() => searchContext.Tickets)
                   .WillReturn(new FakeClassTicketSearchDbSet(GetClassTicketSearchCollection()));
            var classCards = searchAccesor.ClassCardAll();
            Assert.AreEqual(classCards.Count,2,"classcard is mapped properly");
            Assert.AreEqual(classCards[0].FileName,"file1","FlieName is not calculated");
            Assert.AreEqual(classCards[0].Title, "Title1", "FlieName is not calculated");
            Assert.AreEqual(classCards[0].Firstname, "FirstName", "FlieName is not calculated");
            Assert.AreEqual(classCards[0].Id, 1, "id for first row is not calculated");
            Assert.AreEqual(classCards[1].Id, 2, "id for 2nd wor is not calculated");
            Assert.AreEqual(classCards[1].CityId, null, "city id is not calculated");
            Assert.AreEqual(classCards[0].CityId, 1, "city id is not calculated");
            Assert.AreEqual(classCards[1].LocaltyId, null, "location id is not calculated");
            Assert.AreEqual(classCards[0].LocaltyId, 1, "location id is not calculated");

        }

但是当我运行这个单元测试时,它会抛出如下异常

Test method SkillKindle.BLL.Testing.Search.SearchAccessorTest.ClassCardAll_Test_Must_Return_All_ClassCards threw exception: 
System.NullReferenceException: Object reference not set to an instance of an object.
    at lambda_method(Closure, <>f__AnonymousType19`2, <>f__AnonymousType18`9)
   at System.Linq.Enumerable.<SelectManyIterator>d__31`3.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList(IEnumerable`1 source)
   at SkillKindle.BLL.Search.SearchAccessor.ClassCardAll() in SearchAccessor.cs: line 290
   at SkillKindle.BLL.Testing.Search.SearchAccessorTest.ClassCardAll_Test_Must_Return_All_ClassCards() in SearchAccessorTest.cs: line 500
   at TypeMock.MockManager.getReturn(Object context, String typeName, String methodName, Object methodGenericParams, Boolean isDecorated, Boolean isInterceptedType, Object[] methodArguments)
   at Typemock.Interceptors.Profiler.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected, Boolean isInterceptedType)
   at SkillKindle.BLL.Testing.Search.SearchAccessorTest.ClassCardAll_Test_Must_Return_All_ClassCards() in SearchAccessorTest.cs: line 0

我做了一些研究,我开始知道我是否使用

VenueCount = batchVenueTicketData == null ? 0 : (int?) batchVenueTicketData.VenueCount,

代替

VenueCount = batchVenueTicketData.ClassCreativeId == null ? 0 : (int?) batchVenueTicketData.VenueCount,

我的测试工作正常,因为我的查询不起作用

请帮我解决这个问题,在此先感谢

4

0 回答 0