我对使用Rhino Mock进行部分模拟有一个问题:
var authentication = (FormsAuthenticationService)_mocks.PartialMock( typeof(FormsAuthenticationService)); Expect.Call( delegate{ authentication.SetAuthCookie(null, null); }).IgnoreArguments();
…我在“Expect.”行上得到NullReferenceException。。
FormsAuthenticationService 工具 IAuthentication
FormsAuthenticationService
IAuthentication
您是否有充分的理由试图模拟物理类而不是接口?我这样问是因为模仿FormsAuthenticationService存在两个潜在问题:
该类可能没有默认值 在这种情况下,您需要指定一个 重载方法 模拟。部分模拟)。
SetAuthCookie必须是虚拟的。模拟框架通常只能模拟非密封类,并且只能模拟此类的虚拟成员。
var authentication = _mocks.DynamicMock<IAuthentication>(); Expect.Call(() => authentication.SetAuthCookie(null, null)).IgnoreArguments();