对部分模拟的期望 - NullReference异常

时间:2008-11-18 08:13:58

标签: testing rhino-mocks partial-mocks

我使用Rhino Mocks进行部分模拟有问题:

var authentication = (FormsAuthenticationService)_mocks.PartialMock(
  typeof(FormsAuthenticationService));
Expect.Call( delegate{ authentication.SetAuthCookie(null, null); }).IgnoreArguments();

..我在“Expect”上得到NullReferenceException。线..

我将添加FormsAuthenticationService实现IAuthentication

1 个答案:

答案 0 :(得分:1)

你是否有充分的理由试图模拟物理类,而不是界面?我问这个是因为模拟FormsAuthenticationService有两个潜在的问题:

  1. 该课程可能没有默认值 无参数构造函数(其中 case,你需要指定一个 重载方法 mocks.PartialMock)。

  2. SetAuthCookie必须是虚拟的。模拟框架通常只能模拟非密封类,而只能模拟这类类的虚拟成员。

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

    var authentication = _mocks.DynamicMock<IAuthentication>();
    Expect.Call(() => authentication.SetAuthCookie(null, null)).IgnoreArguments();
    
相关问题