We are using Automappers ProjectTo method to build a DTO object that is a subset of a larger database view. It is working as expected when running the actual application but we have had a problem where it doesn't give expected results when unit testing with an EF Core In-Memory database. It seems to just be giving back 0 results no matter what the query. Here is the test I am trying to run.
[Fact]
public async Task GetTemplateAdHocReportList_ReturnsOnlyTemplateReports()
{
await TestHelper.SeedFull(ReportContext); // Calls SeedAdHocReports below along with other seed methods
var results = await _sut.GetTemplateAdHocReports();
results.Where(x => !x.IsTemplate).Count().Should().Be(0);
}
This is the Seed Data:
public static async Task SeedAdHocReports(ReportContext context)
{
var reports = new AdHocReport[]
{
new AdHocReport()
{
Id = 1,
Name = "DevExtreme Example Report",
IsTemplate = true,
AdHocDataSourceId = 1,
Fields = "[{\"caption\":\"Category\",\"dataField\":\"ProductCategoryName\",\"expanded\":true,\"area\":\"row\"},{\"caption\":\"Subcategory\",\"dataField\":\"ProductSubcategoryName\",\"area\":\"row\"},{\"caption\":\"Product\",\"dataField\":\"ProductName\",\"area\":\"row\"},{\"caption\":\"Date\",\"dataField\":\"DateKey\",\"dataType\":\"date\",\"area\":\"column\"},{\"caption\":\"Amount\",\"dataField\":\"SalesAmount\",\"summaryType\":\"sum\",\"format\":{\"type\":\"currency\",\"precision\":2,\"currency\":\"USD\"},\"area\":\"data\"},{\"caption\":\"Store\",\"dataField\":\"StoreName\"},{\"caption\":\"Quantity\",\"dataField\":\"SalesQuantity\",\"summaryType\":\"sum\"},{\"caption\":\"Unit Price\",\"dataField\":\"UnitPrice\",\"format\":\"currency\",\"summaryType\":\"sum\"},{\"dataField\":\"Id\",\"visible\":false}]",
Status = true
}
};
context.AdHocReports.AddRange(reports);
await context.SaveChangesAsync();
}
Here is the GetTemplateAdHocReports method being tested
public async Task<IList<AdHocReportDto>> GetTemplateAdHocReports()
{
//This gives the expected 1 object in the unit tests:
var test = await _reportContext.AdHocReports.Where(x => x.Status && x.IsTemplate).OrderBy(x => x.Name).ToListAsync();
//This always comes back with a count of 0 even though the seed data should return 1 result
var results = await _reportContext.AdHocReports.Where(x => x.Status && x.IsTemplate).OrderBy(x => x.Name).ProjectTo<AdHocReportDto>(_mapper.ConfigurationProvider).ToListAsync();
return results;
}
Finally in case they are useful here are the constructors:
public AdHocServiceTests()
{
TestHelper = new TestHelper();
var reportOptions = TestHelper.GetMockedReportDbOptions();
ReportContext = new ReportContext(reportOptions);
_sut = new AdHocService(ReportContext, TestHelper.Mapper, TestHelper.GetMockedNiceService().Object);
}
public TestHelper()
{
var mappingConfig = new MapperConfiguration(cfg =>
{
cfg.AddProfile<ReportDtoMapperProfile>();
});
Mapper = mappingConfig.CreateMapper(); // public property on TestHelper
}
public AdHocService(ReportContext reportContext, IMapper mapper, INiceService niceService)
{
_niceService = niceService;
_mapper = mapper;
_reportContext = reportContext;
}
So the final question is why does the "var test = " line above work, but the following "var results =" line with the ProjectTo just keeps giving back 0 results?