如何查找已传递给我的模拟(Moq 或 Rhino Mocks)接口上的方法的值?

发布于 2024-08-12 10:31:55 字数 646 浏览 2 评论 0原文

我正在使用Moq - 但可以轻松地切换到另一个模拟框架如果需要

我定义了一个接口:

public interface IBaseEngineManagerImp
{
   void SetClientCallbackSender(IClientCallbackSender clientCallbackSender);
}

然后我用 engineManager 模拟 IBaseEngineManagerImp

mockEngineManagerImp = new Mock<IEngineManagerImp>();
EngineManager engineManager = new EngineManager(mockEngineManagerImp.Object);

,然后调用 SetClientCallbackSender 传入一个值。

如何获取从单元测试传递给 SetClientCallbackSender 的值?

(我希望在 clientCallbackSender 上调用一些方法作为测试的一部分)

I am using Moq - but could easily swap to another mock framework if needed.

I have a interface defined:

public interface IBaseEngineManagerImp
{
   void SetClientCallbackSender(IClientCallbackSender clientCallbackSender);
}

I then mock IBaseEngineManagerImp with

mockEngineManagerImp = new Mock<IEngineManagerImp>();
EngineManager engineManager = new EngineManager(mockEngineManagerImp.Object);

engineManager then calls SetClientCallbackSender passing in a value.

How do I get the value that was passed to SetClientCallbackSender from my unit test?

(I wish to call some methods on clientCallbackSender as part of the test)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放飞的风筝 2024-08-19 10:31:55

您可以在模拟上使用 .Callback 方法,以调用传入 SetClientCallbackSender 方法的参数上的任何函数:

mockEngineManagerImp.Setup(x => x.SetClientCallbackSender(It.IsAny<IClientCallbackSender>()))
            .Callback((IClientCallbackSender c) => c.DoSomething());

you can use the .Callback method on the mock, to call any function on the parameter that was passed in to the SetClientCallbackSender method:

mockEngineManagerImp.Setup(x => x.SetClientCallbackSender(It.IsAny<IClientCallbackSender>()))
            .Callback((IClientCallbackSender c) => c.DoSomething());
忆梦 2024-08-19 10:31:55

在rhino中,您使用WhenCalledGetArgumentsForCallsmadeOn

Thingy argument;
mock
  .Stub(x => x.SetClientCallbackSender(Arg<IClientCallbackSender>.Is.Anything))
  .WhenCalled(call => argument = (Thingy)call.Arguments[0]);
// act
//...
// assert
Assert.AreEqual(7, argument.X); 

此实现的问题是,您只获得最新的参数。您可以通过使用参数约束(而不是 Is.Anything)对此进行更多控制。

// act
//...
// assert
Thingy argument = 
  (Thingy)mock
    .GetArgumentsFormCalsMadeOn(x => x.SetClientCallbackSender(
      Arg<IClientCallbackSender>.Is.Anything))[0][0];

GetArgumentsFormCalsMadeOn 的问题是,它返回一个二维数组,每个调用对应一行,每个参数对应一列。因此,您必须确切地知道被测设备执行了多少次调用。

In rhino, you use WhenCalled or GetArgumentsForCallsmadeOn:

Thingy argument;
mock
  .Stub(x => x.SetClientCallbackSender(Arg<IClientCallbackSender>.Is.Anything))
  .WhenCalled(call => argument = (Thingy)call.Arguments[0]);
// act
//...
// assert
Assert.AreEqual(7, argument.X); 

The problem with this implementation is, that you just get the latest argument. You could put more control to this by using argument contraints (instead of Is.Anything).

or

// act
//...
// assert
Thingy argument = 
  (Thingy)mock
    .GetArgumentsFormCalsMadeOn(x => x.SetClientCallbackSender(
      Arg<IClientCallbackSender>.Is.Anything))[0][0];

The problem with the GetArgumentsFormCalsMadeOn is, that it returns a two dimensional array, a row for each call and a column for each argument. So you have to know exactly how many calls your unit under test performs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文