Entity framework returns null only with include, despite that objects are in data base

Viewed 64

I have strange situation. I write tests , which needs dependency injection because tested class is big. I have something like this : ( I will not paste all code because it is too big )

  1. Register needed Interfaces and Classes
  2. In SetUp (I Use NSubstitute) I resolve them and create new object which is my SUT object, tests are called from this service
  3. In setup I have MockDbContext method when I add data to db and save it. In my case there is something like that :
var rounds = TestSetupHelper.GetMockedSet(DataSyncServiceTestsData.GetRounds());
            var assets = TestSetupHelper.GetMockedSet(DataSyncServiceTestsData.GetAssets());
            var roundAssets = TestSetupHelper.GetMockedSet(DataSyncServiceTestsData.GetRoundAssets());
            var reports = TestSetupHelper.GetMockedSet(DataSyncServiceTestsData.GetReportsData());
            var taskOccurrences = TestSetupHelper.GetMockedSet(DataSyncServiceTestsData.GetTaskOccurrences());
            var measurements = TestSetupHelper.GetMockedSet(DataSyncServiceTestsData.GetMeasurements());
            var included = TestSetupHelper.GetMockedSet(DataSyncServiceTestsData.GetIncludedRoundMeasurements());
            var reportsMeasurments = TestSetupHelper.GetMockedSet(DataSyncServiceTestsData.GetReportMeasurementsData());
            var assetOccurrences = TestSetupHelper.GetMockedSet(DataSyncServiceTestsData.GetAssetOccurrences());
            var areaOccurrences = TestSetupHelper.GetMockedSet(DataSyncServiceTestsData.GetAreaOccurrences());

            var powerplants = DataSyncServiceTestsData.GetPowerPlantsDataOnly();

            var parameterTypes = DataSyncServiceTestsData.GetParameterTypesDataOnly();
            var roundOccurrences = DataSyncServiceTestsData.GetRoundOccurrences();
            var signatures = DataSyncServiceTestsData.GetSignatures();

            dbContext = new AppDbContext(GetInMemoryDbContextOptions());

            dbContext.TaskOccurrences.AddRange(taskOccurrences);
            dbContext.Signatures.AddRange(signatures);
            dbContext.RoundAssets.AddRange(roundAssets);
            dbContext.AreaOccurrences.AddRange(areaOccurrences);

            dbContext.SaveChanges();

            var checker = dbContext.TaskOccurrences; // here we got 18 objects, exactly as we want to add
            var checker3 = dbContext.TaskOccurrences.Include(x => x.RoundOccurrence).FirstOrDefault(x => x.Id == 5); //HERE we have object with id 5

But then, I run test and method goes to querier when I have :

// THIS RETURNS 18 OBJECTS as we expected
var checker1 = _queryContext.TaskOccurrences.toList();

//returns 0, despite that there are TaskOccurrences in _queryContext
            var checker2 = _queryContext.TaskOccurrences
                .Include(x => x.RoundOccurrence).ToList();
// this RETURNS NULL
            var checker3 = _queryContext.TaskOccurrences
                .Include(x => x.RoundOccurrence)
                .FirstOrDefault(x => x.Id == taskOccurrenceId);

// this RETURNS NULL 
            var entity = _queryContext.TaskOccurrences
                .AsNoTracking()
                .Include(x => x.RoundOccurrence)
                .FirstOrDefault(x => x.Id == taskOccurrenceId);

Looks like Include method resets all to null... I don't know why, I have many tests in the same way in my app , and include works fine... What can be wrong here?

0 Answers
Related