我使用Moq來模擬GetCollection
方法,但行崩潰。Mocking MongoCollection崩潰 - 異常已被調用的目標拋出
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()
的熱點來解決它的任何想法的錯誤?
如果你使用一個try/catch,你能趕上任何錯誤?通常,這種失敗通常表示'GetCollection'不能被覆蓋。 – Tejs
@Tejs噢。謝謝!這是錯誤'{「類型'System.ArgumentOutOfRangeException'的異常被拋出。\ r \ n參數名稱:name」}' – zsong
這聽起來像'GetCollection',實際的方法定義,正在運行,或者它調用。我懷疑你的模擬是不夠的。 – Tejs