I am trying to create unit tests(MSTest v2) for a DAL library(EF core)
DataService
public IQueryable<BalanceDTO> GetCollection()
{
var entities = dbContext.Balance;
var dtos = mapper.Map<ICollection<BalanceDTO>>(entities).ToList();
dtos.ForEach(_d =>
{
_d.MonthSort = _d.Date.Month;
_d.MonthName = (new DateTimeFormatInfo()).GetMonthName(_d.MonthSort);
});
return dtos.AsQueryable();
}
public async Task<IList<BalanceDTO>> GetBalancesByYear(int year)
{
return await GetCollection().Where(_d => _d.Date.Year == year).OrderBy(_d => _d.MonthSort).ToListAsync();
}
Test
[TestMethod()]
[DataTestMethod]
[DataRow(2020, 2019)]
public void GetBalancesByYearTest(int found, int notfound)
{
var _configuration = new ConfigurationBuilder()
.SetBasePath(AssemblyProperties.AssemblyDirectory)
.AddJsonFile("appsettings.json")
.Build();
var optionsBuilder = new DbContextOptionsBuilder<AccountManagerContext>();
optionsBuilder.UseSqlServer(_configuration.GetConnectionString("AccountManagerLocalDB"));
var balanceDataService = new BalanceDataService(optionsBuilder);
var elementsFound = balanceDataService.GetBalancesByYear(found);
var elementsNotFound = balanceDataService.GetBalancesByYear(notfound);
Assert.IsNotNull(balanceDataService);
Assert.IsTrue(elementsFound.Result.Count > 0);
Assert.IsTrue(elementsNotFound.Result.Count == 0);
}
But I get this error:
InvalidOperationException: The source IQueryable doesn't implement IAsyncEnumerable<AccountManager.DAL.DTO.BalanceDTO>.
Only sources that implement IAsyncEnumerable can be used for Entity Framework asynchronous operations.
I have found a couple of link but couldn't figure out how to resolve this.
ToArrayAsync() throws "The source IQueryable doesn't implement IAsyncEnumerable"
Any idea about how to create tests for my DataService methods?