unit test failed but LINQ query is returning result in Linq Pad and in Program too

601 views Asked by At

Hi i am writing a query to my Database to get some records using left join and group by and hence i am getting some objects with null value, that is creating problem

below is my query

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

                        }

this query is working fine but its unit test is failing

given below is unit test for it

 [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");

        }

but when I am running this unit test it is throwing exception as below

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

i have done some research and i came to know if i use

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

instead of

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

my test works fine where as my query doesnt work

please help me solving this issue, Thanks in advance

0

There are 0 answers