【问题标题】:Expectations on Partial Mock - NullReference Exception对部分模拟的期望 - NullReference 异常
【发布时间】:2008-11-18 08:13:57
【问题描述】:

我在使用 Rhino Mocks 进行部分模拟时遇到问题:

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

..我在“期望”上得到 NullReferenceException。行..

我将添加 FormsAuthenticationService 实现 IAuthentication

【问题讨论】:

    标签: testing rhino-mocks partial-mocks


    【解决方案1】:

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

    1. 该类可能没有默认值 无参数构造函数(其中 情况下,您需要指定一个 重载的方法 模拟.PartialMock)。

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

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

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

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 2011-10-11
      • 2020-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      • 1970-01-01
      相关资源
      最近更新 更多