Rhino Mocks:AssertNotCalled(加载时除外)

发布于 2024-12-29 16:31:04 字数 506 浏览 1 评论 0原文

我有一个依赖项,当创建我正在测试的对象时会调用该依赖项。但是,此后决不应该调用它。我将如何编写这样的测试?

我只想将这一行作为我的测试(因为我试图遵循 AAA 测试编写风格)。但是,此断言将失败,因为在设置期间调用了 Publish 方法。

Notifier.AssertWasNotCalled(Sub(n) n.Publish(Arg(Of Message).Is.Anything))

有没有办法“重置”对我模拟的依赖项的调用?

注意:我可以对其进行设置,以便在初始化后检查 Message 参数上的属性以获得我期望的值,但这使我的测试更加脆弱/脆弱,并且我如果可能的话想避免它。

_notifier.AssertWasNotCalled(
  Sub(n) n.Publish(Arg(Of Message).Matches(Function(m) m.property = "yo!")))

I have a dependency that gets called when the object I'm testing is created. However, it should never be called after that. How would I write such a test?

I'd like just this line as my test (since I'm trying to follow the AAA style of test writing). However, this assertion will fail since the Publish method was called during setup.

Notifier.AssertWasNotCalled(Sub(n) n.Publish(Arg(Of Message).Is.Anything))

Is there a way to "reset" the calls on the dependency I've mocked?

Note: I can set it up so that I check the property on the Message argument for a value I expect after initialization, but that makes my test more fragile/brittle and I'd like to avoid it if possible.

_notifier.AssertWasNotCalled(
  Sub(n) n.Publish(Arg(Of Message).Matches(Function(m) m.property = "yo!")))

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

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

发布评论

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

评论(1

谜兔 2025-01-05 16:31:04

我将这样做:

_notifier.AssertWasCalled(function(n) p.Publish, function(c) c.Repeat.Once().IgnoreArguments());

因为这将确保它仅被调用一次,这将由您所指示的设置代码触发。

Here is how I would do it:

_notifier.AssertWasCalled(function(n) p.Publish, function(c) c.Repeat.Once().IgnoreArguments());

As this would make sure it is called once only, which would be triggered by your setup code as you have indicated.

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