Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I mock MongoDB objects to test my data models?

I'm using the following logic (MOQ) to attempt to mock out the MongoDB csharp driver objects:

var svr = new Mock<MongoServer>(new MongoServerSettings());
var db = new Mock<MongoDatabase>(svr.Object, new MongoDatabaseSettings("hf_test", 
new MongoCredentials("hf_test", "hf_pass"), GuidRepresentation.Standard,
                                                 SafeMode.False, false));

When I call db.Object, MOQ attempts to create an instance of my mock MongoDatabase, but it fails with a null-reference exception.

Note: I'm thinking of making an IMongoCollection interface, and wrapping MongoCollection in an instance of it. Then, I can simply mock that out... But that seems like a whole lot of unnecessary work.

like image 997
Christopher Davies Avatar asked Aug 01 '11 19:08

Christopher Davies


People also ask

What is MongoDB testing?

If you are using Rational® Integration Tester V9. 5.0 or later, you can create MongoDB transports and run tests against them. MongoDB is an open source, document-oriented NoSQL database. MongoDB is different from traditional SQL-based relational databases.

What is Mongomock?

Mongomock is a small library to help testing Python code that interacts with MongoDB via Pymongo. To understand what it's useful for, we can take the following code: def increase_votes(collection): for document in collection.find(): collection.update(document, {'$set' : {'votes' : document['votes'] + 1}})

Which of the following methods can be used in MongoDB for relation documents?

In MongoDB, you can create a relationship using the following methods: Embedded Relationships. Documented Reference Relationships.

Which of the following command can be used to check the size of a collection named posts?

To view the statistics for a collection, including the data size, use the db. collection. stats() method from the mongo shell. Q 22 - Which of the following commands can cause the database to be locked?


2 Answers

I ended up creating my own interfaces which were basically shallow wrappers on top of the Mongo objects. I can mock these interfaces out, and at least test that the proper indices and filters are in my DAL queries.

like image 194
Christopher Davies Avatar answered Oct 21 '22 00:10

Christopher Davies


this is probably no longer actual (and API might have been changed to be a bit more mock friendly), but here is how it can be done (using Moq):

        var message = string.Empty;
        var server = new Mock<MongoServer>(new MongoServerSettings());
        server.Setup(s => s.IsDatabaseNameValid(It.IsAny<string>(), out message)).Returns(true);

        var database = new Mock<MongoDatabase>(server.Object, "test", new MongoDatabaseSettings()
        {
            GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard,
            ReadEncoding = new UTF8Encoding(),
            ReadPreference = new ReadPreference(),
            WriteConcern = new WriteConcern(),
            WriteEncoding = new UTF8Encoding()
        });

        var mockedDatabase = database.Object;

Main problem here is, that MongoDatabase object calls method from MongoServer inside it's constructor to check, if name of database complies with rules.

Another issue is, that MongoDatabaseSettings should be initialized with all the values (since MongoDatabase constructor tries to check these against defaults provided from server).

Biggest issue is, that this mocking code might fall apart when new release of c# driver is released :). So writing wrappers on top of Mongo might be actually best fit.

like image 1
Marian Polacek Avatar answered Oct 21 '22 01:10

Marian Polacek