I have an async method:
public async Task<TProjection> GetById<TProjection>(int id)
{
return await Collection
.Where(entity => entity.Id == id)
.ProjectTo<TProjection>()
.ToListAsync();
}
That I need to unit test, which I am doing thanks to the answer here
However, attempting to project to a TestEntity causes the following exception to be thrown:
Unable to cast object of type 'TestDbAsyncEnumerable'1[Models.Entity]' to type 'System.Linq.IQueryable'1[TestSpecNamespace+TestEntity]'.
Here is the test:
[Theory]//And then a bunch of ids
public async Task GetSubEntities_Projects_IdExists(int id)
{
Mapper.Initialize(cfg => { cfg.CreateMap<Entity, TestEntity>(); });
var mockContext = CreateMockContext();
var provider = TestProvider.Create(context: mockContext.Object);
var response = await provider.GetById<TestEntity>(id); //Calls the aforementioned
//Asserts
}