I have a data layer that uses the C# MongoDB.Driver.
My data layer has a public MongoDatabase so I can inject a mock MongoDatabase for testing.
I'd like to test my datalayer by injecting a mock MongoDatabase and returning a mocked MongoCollection.
I've tried a number of ways to do this from the 2 or 3 suggestions made on the Web.Latest version is adapted from http://thoai-nguyen.blogspot.com.au/2012/06/how-i-mock-mongo-collection.html which mocks a collection and not a database
[Test()]public void GetAllSettingsTest(){//ArrangeMongoServerSettings ServerSettings;MongoServer Server;MongoDatabaseSettings DatabaseSettings;MongoDatabase Database;
ServerSettings = new MongoServerSettings{Servers = new List<MongoServerAddress>{new MongoServerAddress("unittest")}};
Server = new MongoServer(ServerSettings);DatabaseSettings = new MongoDatabaseSettings("databaseName",new MongoCredentials("", ""),GuidRepresentation.Standard,ReadPreference.Nearest,SafeMode.True);Database = new MongoDatabase(Server, DatabaseSettings);
var collectionSetting = new MongoCollectionSettings<BsonDocument>(Database, typeof(BsonDocument).Name);var collection = new Mock<MongoCollection<BsonDocument>>(Database, collectionSetting);
collection.Setup(f => f.Database).Returns(Database);collection.Setup(f => f.Settings).Returns(collectionSetting);
BsonDocument doc01 = new BsonDocument();BsonDocument doc02 = new BsonDocument();collection.Object.Insert(doc01);collection.Object.Insert(doc02);
var mongoDatabase = new Mock<MongoDatabase>(Server, DatabaseSettings);mongoDatabase.Setup(f => f.GetCollection(MongoCollections.Settings)).Returns(collection.Object);
ILogger logger = new Logger();DatabaseClient.DatabaseClient target = new DatabaseClient.DatabaseClient(logger);target.MongoDatabase = mongoDatabase.Object;
//ActList<BsonDocument> resultList = target.GetAllSettings();}
At the line target.MongoDatabase = mongoDatabase.Object; I'm getting a TargetInvocationException which is coming from the Object of the mock.I'm thinking this is a problem with constructing the object even though I have supplied the parameter objects.
Anyone know what I'm doing wrong or a better way to do this?
The method I'm testing is:
public List<BsonDocument> GetAllSettings(){var collection = MongoDatabase.GetCollection<BsonDocument>(MongoCollections.Settings);var query = from e in collection.AsQueryable()select e;
var settings = query.ToList();return settings;}--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
You mean you're writing unit tests for your database code: You write your own DAO layer to accommodate alternate testing such that nothing reaches MongoDB, no MongoDB connection need actually be set up, and canned (stubbed) responses are returned that you can verify. The purpose is to test your code, not MongoDB. Leave off testing your database to integration testing.