How do I Moq IFindFluent so this call to ToListAsync works?
I wound up making a little abstraction layer for this, since I couldn't find anything suitable and asking for help yielded no answers.
I created an interface/implementation pair called AppCollection
specifically to handle the MongoDB interface. IAppCollection
would have a method for IAppCollection.ToList(predicate)
, and the AppCollection
would run the Collection.Find(predicate).ToListAsync();
call, returning the list. Later, it was a trivial matter to mock the IAppCollection
to make sure the right calls were being made. While I couldn't test the predicates in native LINQ, I could at least compile the predicates and compare them to passing/failing objects.
If anyone would struggle with getting it to work, what I did to mock Find()
method was:
[TestFixture]
class QueryControllerTests
{
private IOptions<MongoSettings> _mongoSettings;
private QueryController _queryController;
private Mock<IFakeMongoCollection> _fakeMongoCollection;
private Mock<IFindFluent<BsonDocument, BsonDocument>> _fakeCollectionResult;
[OneTimeSetUp]
public void Setup()
{
_fakeMongoCollection = new Mock<IFakeMongoCollection>();
_fakeCollectionResult = new Mock<IFindFluent<BsonDocument, BsonDocument>>();
}
}
where IFakeMongoCollection
is:
public interface IFakeMongoCollection : IMongoCollection<BsonDocument>
{
IFindFluent<BsonDocument, BsonDocument> Find(FilterDefinition<BsonDocument> filter, FindOptions options);
}