我有一个异步方法:
public async Task<TProjection> GetById<TProjection>(int id)
{
return await Collection
.Where(entity => entity.Id == id)
.ProjectTo<TProjection>()
.ToListAsync();
}
我需要进行单元测试,这要归功于answer here
但是,尝试投影到TestEntity会导致抛出以下异常:
Unable to cast object of type 'TestDbAsyncEnumerable'1[Models.Entity]' to type 'System.Linq.IQueryable'1[TestSpecNamespace+TestEntity]'.
以下是测试:
[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
}