我将如何使用 moq 来测试 MongoDB 服务层? [英] How would I use moq to test a MongoDB service layer?
问题描述
我的应用程序和 mongo 数据库之间有一个服务层.
I have a service layer between my app and the mongo database.
我正在尝试使用 moq 构建单元测试我对起订量很陌生,所以我从我认为是微不足道的测试开始.
I'm trying to build a unit test using moq I'm quite new to moq so I started with what I thought would be a trivial test.
要测试的代码:
public List<BsonDocument> GetAllSettings()
{
var collection = MongoDatabase.GetCollection<BsonDocument>("Settings");
var query = from e in collection.AsQueryable()
select e;
var settings = query.ToList();
return settings;
}
哪里:设置是一个集合MongoDatabase 是一个 MongoDBDriver.MongoDatabase
Where: Settings is a Collection MongoDatabase is a MongoDBDriver.MongoDatabase
我试过这个作为我的测试:
I've tried this as my test:
[Test()]
public void GetAllSettingsTest()
{
//Arrange
BsonDocument doc01 = new BsonDocument();
BsonDocument doc02 = new BsonDocument();
var mongoDatabase = new Mock<MongoDatabase>();
var collection = new Mock<MongoCollection<BsonDocument>>();
mongoDatabase.Setup(f => f.GetCollection(MongoCollection.Settings)).Returns(collection.Object);
collection.Object.Insert(doc01);
collection.Object.Insert(doc02);
ILogger logger = new Logger();
DatabaseClient.DatabaseClient target = new DatabaseClient.DatabaseClient(logger);
target.MongoDatabase = mongoDatabase.Object;
MongoCursor<BsonDocument> cursor = collection.Object.FindAllAs<BsonDocument>();
List<BsonDocument> expected = cursor.ToList();
List<BsonDocument> actual;
//Act
actual = target.GetAllSettings();
//Assert
Assert.AreEqual(expected, actual);
}
我在以下位置收到找不到无参数构造函数"的错误:
I'm getting an error of "Could not find a parameterless constructor" at:
mongoDatabase.Setup(f => f.GetCollection(MongoCollections.Settings)).Returns(collection.Object);
mongoDatabase.Setup(f => f.GetCollection(MongoCollections.Settings)).Returns(collection.Object);
错误是指 MongoCollection 对象.我不认为它有构造函数.
The error refers to the MongoCollection object. I didn't think it had a constructor.
我该怎么做才能运行我的测试?
What can I do to get my test to run?
推荐答案
这个问题很可能与:如何模拟 MongoDB 对象来测试我的数据模型?
无论如何,这是模拟所需的最小起订量配置
Anyway, here is minimal Moq configuration required to mock
var message = string.Empty;
var serverSettings = new MongoServerSettings()
{
GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard,
ReadEncoding = new UTF8Encoding(),
ReadPreference = new ReadPreference(),
WriteConcern = new WriteConcern(),
WriteEncoding = new UTF8Encoding()
};
var server = new Mock<MongoServer>(serverSettings);
server.Setup(s => s.Settings).Returns(serverSettings);
server.Setup(s => s.IsDatabaseNameValid(It.IsAny<string>(), out message)).Returns(true);
var databaseSettings = new MongoDatabaseSettings()
{
GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard,
ReadEncoding = new UTF8Encoding(),
ReadPreference = new ReadPreference(),
WriteConcern = new WriteConcern(),
WriteEncoding = new UTF8Encoding()
};
var database = new Mock<MongoDatabase>(server.Object, "test", databaseSettings);
database.Setup(db => db.Settings).Returns(databaseSettings);
database.Setup(db => db.IsCollectionNameValid(It.IsAny<string>(), out message)).Returns(true);
var mockedCollection = collection.Object;
无论如何,正如我在链接问题中提到的,当 MongoDriver 的任何内部工作发生变化时,这可能没有用.
Anyway, as I mentioned in linked question, this might not be useful when any of inner-workings of MongoDriver change.
这篇关于我将如何使用 moq 来测试 MongoDB 服务层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!