当存根一个简单的只读属性时,RhinoMocks 会抛出 3 个异常。如何在没有异常的情况下做到这一点。
public class ThirdPartyClass
{
public string Name { get; set; }
public ThirdPartyClass(string name)
{
Name = name;
}
}
public interface IWithProperty
{
ThirdPartyClass MyReadonlyProperty { get; }
}
[TestClass]
public class MockedProperty
{
[TestMethod]
public void PropertyTest()
{
IWithProperty withProperty = MockRepository.GenerateStub<IWithProperty>();
// 3 exceptions thrown
withProperty.Stub(wp => wp.MyReadonlyProperty).Return(new ThirdPartyClass("Mocked"));
}
}
这会在 1 行中引发 3 个以下异常:
System.MissingMethodException 发生 Message=Constructor on type 'Castle.Proxies.ThirdPartyClassProxyc32aba1028c144bb844d190d92ad191a' 未找到。Source=mscorlib StackTrace:在 System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr,Binder binder,Object[] args,CultureInfo 文化,Object[] activationAttributes)InnerException:
System.MissingMethodException 发生 Message=Constructor on type 'Castle.Proxies.ThirdPartyClassProxyc32aba1028c144bb844d190d92ad191a' 未找到。Source=mscorlib StackTrace:在 System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr,Binder binder,Object[] args,CultureInfo 文化,Object[] activationAttributes)InnerException:
System.ArgumentException 发生消息=无法实例化类的代理:Test.ThirdPartyClass。找不到无参数构造函数。参数名称:constructorArguments
Source=Rhino.Mocks ParamName=constructorArguments StackTrace:在 Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] 拦截器) InnerException: