Testing 对部分Mock-NullReference异常的期望

Testing 对部分Mock-NullReference异常的期望,testing,rhino-mocks,partial-mocks,Testing,Rhino Mocks,Partial Mocks,我对使用Rhino Mock进行部分模拟有一个问题: var authentication = (FormsAuthenticationService)_mocks.PartialMock( typeof(FormsAuthenticationService)); Expect.Call( delegate{ authentication.SetAuthCookie(null, null); }).IgnoreArguments(); …我在“Expect.”行上得到NullReferen

我对使用Rhino Mock进行部分模拟有一个问题:

var authentication = (FormsAuthenticationService)_mocks.PartialMock(
  typeof(FormsAuthenticationService));
Expect.Call( delegate{ authentication.SetAuthCookie(null, null); }).IgnoreArguments();
…我在“Expect.”行上得到NullReferenceException


我将只添加
FormsAuthenticationService
实现
IAAuthentication

您是否有充分的理由试图模拟物理类而不是接口?我这样问是因为模仿FormsAuthenticationService存在两个潜在问题:

  • 该类可能没有默认值 无参数构造函数(其中 在这种情况下,您需要指定一个 重载方法 模拟。部分模拟)

  • SetAuthCookie必须是虚拟的。模拟框架通常只能模拟非密封类,并且只能模拟此类的虚拟成员

  • 为了解决这些问题,我建议改为模拟IAuthentication。模拟接口没有这些限制。下面是您要编写的代码:

    var authentication = _mocks.DynamicMock<IAuthentication>();
    Expect.Call(() => authentication.SetAuthCookie(null, null)).IgnoreArguments();
    
    var身份验证=_mocks.DynamicMock();
    Expect.Call(()=>authentication.SetAuthCookie(null,null)).IgnoreArguments();