【问题标题】:Rhino Mocks stub only returns nullRhino Mocks 存根仅返回 null
【发布时间】:2016-04-05 12:03:56
【问题描述】:

我已经为此苦恼了一天。互联网上没有关于此的内容。我有一个存根函数。当它在 rhino mocks 中被调用时,它只返回 null 但我希望它返回一个不同的值。看看:

[Test]
public void GetViewingInfo_Throw_WhenViewingIDReturnsNull()
{
     _managementDataController
          .Stub(s => s.GetManagementByID(1))
          .Return(new Info(){ApprovedByEmployeeID = 123});

     Assert.Throws<InvalidOperationException>(() => 
          _managementBusinessController.GetViewingInfo(1));
}

现在基本上.Return(new Info(){ApprovedByEmployeeID = 123}); 总是返回空值。我做错了吗?

这里是GetViewingInfo 代码:

var manage = _managementDataController.GetManagementByID(managementID);
InvalidValueGuard.ThrowInvalidOperationExceptionOn(() => manage == null, "");

我在 InvalidValueGuard 处设置了一个断点,并且管理变量总是返回 null。

【问题讨论】:

  • 那么GetManagementByID 是由GetViewingInfo 调用的吗?你能显示那个代码吗?
  • GetManagementByID 实际上是用1 的参数调用的吗?如果调用不匹配,将返回默认值。

标签: c# rhino-mocks


【解决方案1】:

我通过创建一个具体的类来解决这个问题。

Info info = new Info();

_managementDataController
          .Stub(s => s.GetManagementByID(1))
          .Return(info);

我不知道为什么会这样。我猜当你在 return 方法中发送一个类时,闭包不起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 2011-01-24
    相关资源
    最近更新 更多