I am struggling in mocking IMongoCollection.Find
method using Moq for my unit tests.
What I tried:
Mock<IMongoCollection<Person>> mockIMongoCollection = new Mock<IMongoCollection<Person>>();
mockIMongoCollection.SetupAllProperties();
mockIMongoCollection
.Setup(x => x.Find(
It.IsAny<FilterDefinition<Person>>(),
It.IsAny<FindOptions>()))
.Returns();
The thing is whatever I try to return using Returns()
, it is not working, I expect to be able to return something convertible to List<Person>
and I can't mock or create an instance of IFindFluent<Person,Person>
as suggested by return type of Find
method.
Best Answer
Maby this will help you.
I had to Mock such query:
to do that I created an abstraction for MongoCollection to handle mongo queries.
So my tests setup looks like this
I hope it helps.