Below is the kind of test that is failing upon .ShouldNotThrow() due to .ToListAsync() not being supported by in-memory dbsets (I don't have the exact wording handy but you get the picture). In case it's of any importance, I'm trying to mockup the dbset provided by Entity Framework ver. 6.1.3:
[TestFixture]
public class Tests
{
private SomeRepository _repository;
private Mock<DbSet<SomeEntity>> _mockDbSet;
private Mock<IApplicationDbContext> _mockAppDbContext;
[OneTimeSetUp]
public void TestFixtureSetUp()
{
_mockDbSet = new Mock<DbSet<SomeEntity>>();
_mockAppDbContext = new Mock<IApplicationDbContext>();
_mockAppDbContext.SetupGet(c => c.Gigs).Returns(_mockGigsDbSet.Object);
_repository = new SomeRepository(_mockAppDbContext.Object);
}
[Test]
public void Test()
{
// Setup
var results = (IEnumerable<SomeEntity>) null;
var singleEntity = new SomeEntity {Id = "1"};
_mockDbSet.SetSource(new List<SomeEntity> { singleEntity });
// Act
var action = new Func<Task>(async () =>
{
results = await _repository.GetMultipleAsync(); //this ends up calling "await mockDbSet.ToListAsync().ConfigureAwait(false)" internally
});
// Verify
action.ShouldNotThrow(); //an exception is thrown about .ToListAsync() not being supported by in-memory dbsets or something to that effect
results.Should().BeEmpty();
}
}
The above test works as intended if .ToList() is used synchronously in place of the async-based .ToListAsync(). Also the repository works fine when used from within the actual asp.net.
So what's the correct way to go about mocking up the dbset for .ToListAsync() to work in these unit-tests?
P.S.: The project I've been unit-testing can be found here:
https://bitbucket.org/dsidirop/gighub
The unit-tests that fail due to .ToListAsync() are marked with a comment 'fails for the time being'.