I am using .NET Core 2.0 and the .NET Core MongoDB driver.
I have created a repository like so:
public interface IRepository<T>
{
IMongoQueryable<T> Get()
}
I have done this to give flexibility to whoever uses this to be able to do LINQ much like they would do using EF. The problem is when it comes to unit testing and I'm trying to create an in-memory database so I can check states before and after operation.
Some stuff I tried:
public class InMemoryRepository : IRepository<ConcreteType>
{
private HashSet<ConcreteType> _data = new HashSet<ConcreteType>();
public IMongoQueryable<ConcreteType> Get()
{
return (IMongoQueryable<ConcreteType>)_data.AsQueryable();
}
}
The case doesn't work as the interface for IMongoQueryable
is:
public interface IMongoQueryable<T> : IMongoQueryable, IQueryable, IEnumerable, IQueryable<T>, IEnumerable<T>, IAsyncCursorSource<T>
Another go:
public class InMemoryRepository : IRepository<ConcreteType>
{
private HashSet<ConcreteType> _data = new HashSet<ConcreteType>();
public InMemoryRepository()
{
_mongoQueryableMock = new Mock<IMongoQueryable<ConcreteType>>();
_mongoQueryableMock.Setup(m => m.AsQueryable()).Returns(_data.AsQueryable);
}
public IMongoQueryable<ConcreteType> Get()
{
return _mongoQueryableMock.Object;
}
}
This doesn't work as IMongoQueryable.AsQueryable()
is an extension method and I can't mock/setup that.