如何设置将在 Rhino Mocks 中的另一个线程上进行的调用的期望

发布于 2024-07-07 03:11:21 字数 817 浏览 4 评论 0原文

我有一个类,旨在启动后台线程,将从该线程调用管理器。 为了单元测试的目的,该经理将被嘲笑。 相关的代码片段是:

 MockRepository mocks = new MockRepository();
ICacheManager manager = mocks.CreateMock<ICacheManager>();

Expect.On(manager).Call(manager.CacheSize).Return(100);
mocks.ReplayAll();

CacheJanitor janitor = new CacheJanitor(manager);

// get janitor to do its stuff
// ...

mocks.VerifyAll();

问题是,在验证时,我们抛出了两个异常 - 一个在测试线程上,表明预期会调用 CacheSize 但没有发生,另一个在后台线程(在 CacheJanitor 内)指出发生了对 CacheSize 的调用,但不是预期的。

显然,期望与创建它们的线程有密切关系。 有谁知道如何指示 Rhino Mocks 期望在不同线程上进行调用(在定义期望时甚至不存在)?

编辑:

忘记提及我们在不久的将来仍然使用 VS 2005 的限制。 Rhino Mocks 版本是 3.4 - 我将尝试使用 3.5,但改进列表似乎没有表明该领域有任何修复。

目前,我可能会为这一系列测试创建自己的模拟对象,并在其中记录结果,但肯定会感谢任何允许我使用 Rhino Mocks 干净地实现这一点的解决方案。

I have a class which is designed to spin up a background thread, from which calls will be made into a manager. This manager will be mocked for the purposes of unit test. The relevant fragment of code is:

 MockRepository mocks = new MockRepository();
ICacheManager manager = mocks.CreateMock<ICacheManager>();

Expect.On(manager).Call(manager.CacheSize).Return(100);
mocks.ReplayAll();

CacheJanitor janitor = new CacheJanitor(manager);

// get janitor to do its stuff
// ...

mocks.VerifyAll();

The problem is that when verified we get two exceptions thrown - one on the test thread stating that a call to CacheSize was expected but didn't occur, and another on the background thread (within CacheJanitor) stating that a call to CacheSize occurred but was not expected.

Obviously the expectations have an affinity to the thread on which they are created. Does anyone know of a way to instruct Rhino Mocks to expect the call on a different thread (which doesn't even exist at the time that the expectations are defined)?

EDIT:

Forgot to mention the constraint that we are still on VS 2005 for the immediate future. Rhino Mocks version is 3.4 - I will try with 3.5, but the list of improvements don't seem to indicate any fixes in this area.

For the moment I will probably create my own mocked object for this series of tests and record the results within that, but would definately appreciate any solutions which allow me to cleanly achieve this using Rhino Mocks.

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

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

发布评论

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

评论(1

半世蒼涼 2024-07-14 03:11:21

哇。 太疯狂了。 我已经使用最小起订量来完成此类事情,没有出现任何问题。

您使用的是哪个版本的犀牛? 您以 3.5 之前的方式使用它; 也许这不会是 3.5 版本的问题?

另一种选择是避免验证模拟的期望。 (抱歉,我不熟悉Rhino的语法,但也许是可能的)。 使用回调在测试函数中设置局部变量并验证该变量。

在最小起订量中,我会这样做:

// Arrange
var called = false;
var mock = new Mock<SomeObject>();            

mock.Expect(x => SomeFunction()).Callback(() => called = true);            

var target = new MultithreadedTarget(mock.Object);            

// Act
target.DoSomethingThatCallsSomeFunctionOnAnotherThreadLol();            

// this part is a bit ugly
// spinlock for a bit to give the other thread a chance
var timeout = 0;
while(!called && timeout++ < 1000)
  Thread.Sleep(1);

// Assert
Assert.IsTrue(called);

Wow. That's crazy. I've used MoQ to do this sort of thing without an issue.

What version of Rhino are you using? You're using it in the pre-3.5 manner; perhaps this wouldn't be an issue with version 3.5?

Another alternative is to avoid verifying expectations on the mocks. (sorry, I'm not familiar with Rhino's syntax but it might be possible). Use a callback to set a local variable in the test function and verify on that variable.

In MoQ, I'd do it like this:

// Arrange
var called = false;
var mock = new Mock<SomeObject>();            

mock.Expect(x => SomeFunction()).Callback(() => called = true);            

var target = new MultithreadedTarget(mock.Object);            

// Act
target.DoSomethingThatCallsSomeFunctionOnAnotherThreadLol();            

// this part is a bit ugly
// spinlock for a bit to give the other thread a chance
var timeout = 0;
while(!called && timeout++ < 1000)
  Thread.Sleep(1);

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