In memory / fake implementation of IMongoCollection<T>?

1,464 views
Skip to first unread message

Daniel Stockhammer

unread,
Jun 19, 2015, 6:22:26 PM6/19/15
to mongodb...@googlegroups.com
Hi,

I'm using IMongoCollection<T> directly in code that I would like to have covered with unit tests. I don't really like the idea of creating a stub/mock for the collection, since I consider it an implementation which method on the collection is actually called. Apart from that, the interface is quite difficult to mock anyway. I would rather have something like an InMemoryMongoCollection<T> that could be used for unit tests, similar to what Entity Framework provides.

Does a fake implemtation like this already exist somewhere, or does anybody have a recommended approach to solving this problem?
Are there any plans to provide a fully compatible InMemory implementation in the future?

If the answer to all those questions is no, I'm probably going to implement something along those lines myself and put it on Github. Might be useful for other people as well.

Cheers,
Dan

torn...@gmail.com

unread,
Jun 20, 2015, 1:40:43 AM6/20/15
to mongodb...@googlegroups.com
Yes,
i tried something similar, there are atleast two ways in the driver architecture to interfere:
- FieldDefinition, FilterDefinition level: Basically you can try to reverse the Filter/FieldDefinitions back to Expressiontrees then apply to a collection in memory, unfortunately the field/filterdefinitions types are mostly not public so you would need to work quite a bit with reflection or change the driver itself.
- IOperationExecutor: An interface which is used in the unit tests of driver, https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver.Tests/MockOperationExecutor.cs. This is fairly deep in the driver, I did not take a closer look into this. If you go this route you then would apply again against a collection in linq for example.

for the first way you will need to mock every of the highlevel interfaces (e.g. IMongoCollection too), for the second way it might not be necessary.

Maybe this info helps.
Reply all
Reply to author
Forward
0 new messages