gpt4 book ai didi

c# - 模拟 MongoCollection 崩溃 - 调用目标抛出异常

转载 作者:可可西里 更新时间:2023-11-01 09:27:34 26 4
gpt4 key购买 nike

我使用 Moq 来模拟 GetCollection 方法,但该行崩溃了。

var collectionSettings = new MongoCollectionSettings
{
GuidRepresentation = GuidRepresentation.Standard,
ReadEncoding = new UTF8Encoding(),
ReadPreference = new ReadPreference(),
WriteConcern = new WriteConcern(),
WriteEncoding = new UTF8Encoding()
};

var collection = new Mock<MongoCollection<BsonDocument>>(database.Object, "MyCollection", collectionSettings);

//crashing here without any error dumped
database.Setup(f => f.GetCollection("MyCollection", collectionSettings)).Returns(collection.Object);

这是我得到的错误

Exception of type 'System.ArgumentOutOfRangeException' was thrown.
Parameter name: name
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxyInstance(Type proxyType, List`1 proxyArguments, Type classToProxy, Object[] constructorArguments)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)
at Moq.Proxy.CastleProxyFactory.CreateProxy(Type mockType, ICallInterceptor interceptor, Type[] interfaces, Object[] arguments)
at Moq.Mock`1.<InitializeInstance>b__0()
at Moq.PexProtector.Invoke(Action action)
at Moq.Mock`1.InitializeInstance()
at Moq.Mock`1.OnGetObject()
at Moq.Mock.GetObject()
at Moq.Mock.get_Object()
at Moq.Mock`1.get_Object()

关于热修复它有什么想法吗?

最佳答案

已接受的答案不适用于 C# 驱动程序版本 1.9.1,并使用了 OP 引入的一些未公开类型。我已经更改了允许模拟 MongoCollection<T> 的辅助方法。 :

public static class MongoMock
{
public static Mock<MongoServer> CreateMongoServer()
{
var serverSettings = new MongoServerSettings
{
Servers = new List<MongoServerAddress>
{
new MongoServerAddress("MockServer")
}
};

var server = new Mock<MongoServer>(MockBehavior.Strict, serverSettings);

string message;
server.Setup(s => s.Settings).Returns(serverSettings);
server.Setup(s => s.IsDatabaseNameValid(It.IsAny<string>(), out message)).Returns(true);

return server;
}

public static Mock<MongoDatabase> CreateMongoDatabase(MongoServer server)
{
var databaseSettings = new MongoDatabaseSettings
{
GuidRepresentation = GuidRepresentation.Standard,
ReadEncoding = new UTF8Encoding(),
ReadPreference = new ReadPreference(),
WriteConcern = new WriteConcern(),
WriteEncoding = new UTF8Encoding()
};
var database = new Mock<MongoDatabase>(MockBehavior.Strict, server, "MockDatabase", databaseSettings);

string message;
database.Setup(db => db.Server).Returns(server);
database.Setup(db => db.Settings).Returns(databaseSettings);
database.Setup(db => db.IsCollectionNameValid(It.IsAny<string>(), out message)).Returns(true);

return database;
}

public static Mock<MongoCollection<T>> CreateMongoCollection<T>(MongoDatabase database, string collectionName)
{
var collectionSetting = new MongoCollectionSettings();
var collectionMock = new Mock<MongoCollection<T>>(MockBehavior.Strict, database, collectionName, collectionSetting);
collectionMock.Setup(x => x.Database).Returns(database);
collectionMock.Setup(x => x.Settings).Returns(collectionSetting);
return collectionMock;
}

public static Mock<MongoCollection<T>> CreateMongoCollection<T>(string collectionName)
{
var server = CreateMongoServer().Object;
var database = CreateMongoDatabase(server);
return CreateMongoCollection<T>(database.Object, collectionName);
}

public static Mock<MongoCursor<T>> CreateMongoCursor<T>(MongoCollection<T> collection, IEnumerable<T> items = null)
{
var cursorMock = new Mock<MongoCursor<T>>(MockBehavior.Strict, collection, null, null, null, null);

if (items != null)
{
cursorMock.Setup(c => c.GetEnumerator()).Returns(items.GetEnumerator());
}

return cursorMock;
}
}

用法:

var serverMock = MongoMock.CreateMongoServer();
var server = serverMock.Object;

var databaseMock = MongoMock.CreateMongoDatabase(serverMock.Object);
var database = databaseMock.Object;

var collectionMock = MongoMock.CreateMongoCollection<BsonDocument>(databaseMock.Object, "FooCollection");
var collection = collectionMock.Object;

var cursorMock = MongoMock.CreateMongoCursor(collectionMock.Object, new List<BsonDocument>());
var cursor = cursorMock.Object;

关于c# - 模拟 MongoCollection 崩溃 - 调用目标抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21148626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com