I am trying to make a FakeDbContext with a FakeDbSet for unit testing.
But I get the following error (see below). I am extending DbSet so normally IDbAsyncEnumerable
Just a note for people using the boilerplate code discussed above from Microsoft, here is a quick helper class that can transform your mocked up data into the async result. Just add to the bottom of the MS code and call with something like
var fakeDateAsMockAsyncQueryResult = new MockAsyncData().MockAsyncQueryResult(fakeDataList.AsQueryable());
.......
internal class MockAsyncData where T : class
{
public Mock> MockAsyncQueryResult(IQueryable data)
{
var mockSet = new Mock>();
mockSet.As>()
.Setup(m => m.GetAsyncEnumerator())
.Returns(new TestDbAsyncEnumerator(data.GetEnumerator()));
mockSet.As>()
.Setup(m => m.Provider)
.Returns(new TestDbAsyncQueryProvider(data.Provider));
mockSet.As>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
return mockSet;
}
}
It's the same code as in the MS example but generic and reusable from many different unit tests.