制定通用和特定的存根方法?

发布于 2024-09-14 21:53:39 字数 957 浏览 6 评论 0原文

为了测试另一个类,我想为接口IFoo创建一个存根:

public interface IFoo
{
    int DoSomething(int value);
}

该存根是在SetUp(或TestInitialize)中创建的- 测试装置的方法并存储在属性中,因为几乎所有测试方法都需要 IFoo:

this.Foo = MockRepository.GenerateStub<IFoo>();

因为 IFoo.DoSomething 在所有情况下只需要返回传递的值,除了我将此行为添加到我的 < em>SetUp 方法:

this.Foo.Stub(f => f.DoSomething(0)).IgnoreArguments().Return(0).WhenCalled(mi => mi.ReturnValue = mi.Arguments[0]);

但是现在,这个 one 测试需要不同的行为:如果传递的值等于另一个常量,则应该返回一个常量,而不是返回传递的参数。以下方法(在测试方法本身中编写)不起作用

this.Foo.Stub(f => f.DoSomething(42)).Return(43);

先前指定的更通用的存根似乎覆盖了更具体的存根。
有什么方法可以在 RhinoMock 对象上指定特定的和通用的存根吗?

一种解决方法是在测试方法本身中重新创建存根 - 我想避免这种重复。不过,我可以接受这个版本。
我也不希望通过约束在一般声明中明确排除“42”,因为这会对其他测试产生负面影响。
调用 BackToRecord 也不是一个选项,因为它似乎也会重置其他存根。

感谢您的回答!

in order to test another class, I want to create a stub for an interface IFoo:

public interface IFoo
{
    int DoSomething(int value);
}

The stub is created in the SetUp (or TestInitialize)-method for the test fixture and stored in a property as almost all test methods need an IFoo:

this.Foo = MockRepository.GenerateStub<IFoo>();

As IFoo.DoSomething needs just to return the passed value in all cases except one I added this behavior to my SetUp method:

this.Foo.Stub(f => f.DoSomething(0)).IgnoreArguments().Return(0).WhenCalled(mi => mi.ReturnValue = mi.Arguments[0]);

Now, however, this one test requires different behavior: Rather than returning the passed argument, a constant should be returned if the passed value equals another constant. The following approach (written in the test method itself) does not work:

this.Foo.Stub(f => f.DoSomething(42)).Return(43);

The previous specified, more general stub seems to override the more specific stub.
Is there any way to specify both specific and general stubs on RhinoMock-objects?

One workaround would be recreating the stub in the test-method itself - a duplication I would like to avoid. I could live with this version, however.
What I also do not want is a explicit exclusion of the "42" in the general statement via Constraints as this will effect the other tests negatively.
Calling BackToRecord is also not an option as it seems to reset other stubs, too.

Thanks for your answers!

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

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

发布评论

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

评论(2

相权↑美人 2024-09-21 21:53:39

我认为不可能“重置”期望。在这种情况下,我会避免在设置中设置此模拟,而是创建一个方法来轻松设置它并从每个测试中调用它:

private IFoo SetUpFoo()
{
  IFoo foo = MockRepository.GenerateStub<IFoo>();
  foo.Stub(f => f.DoSomething(0)).IgnoreArguments().Return(0).WhenCalled(mi => mi.ReturnValue = mi.Arguments[0]);
}

然后只需在需要标准 foo: 的每个测试中使用此方法:

[Test]
public void StandardTest()
{
  IFoo foo = SetUpFoo();
  ...
}

在您的特殊如果你必须手动创建 foo 并设置期望:

[Test]
public void SpecialTest()
{
  IFoo foo = MockRepository.GenerateStub<IFoo>();
  foo.Stub(f => f.DoSomething(42)).Return(43);
  ...
}

当然你会得到一些代码重复,但至少它清楚在哪个测试中使用了哪些期望。

I don't think its possible to "reset" expectations. In this case I would avoid setting up this mock in the set up, instead I would create a method to easily set it up and call it from every test:

private IFoo SetUpFoo()
{
  IFoo foo = MockRepository.GenerateStub<IFoo>();
  foo.Stub(f => f.DoSomething(0)).IgnoreArguments().Return(0).WhenCalled(mi => mi.ReturnValue = mi.Arguments[0]);
}

Then just use this method in every test that needs the standard foo:

[Test]
public void StandardTest()
{
  IFoo foo = SetUpFoo();
  ...
}

in your special case you have to create foo manually and set up the expectation:

[Test]
public void SpecialTest()
{
  IFoo foo = MockRepository.GenerateStub<IFoo>();
  foo.Stub(f => f.DoSomething(42)).Return(43);
  ...
}

Of course you get some code duplication, but at least its clear which expectations are used in which test.

行至春深 2024-09-21 21:53:39

我刚刚找到了实际“覆盖”存根/期望的解决方案:
Rhino Mocks:如何清除先前的期望应用于

我的问题,我现在可以写:

this.Foo.Stub(f => f.DoSomething(42)).Return(43).Repeat.Any();

Any 的文档说:

重复该方法任意次。这具有特殊的影响,因为该方法现在将忽略 orderring。

所以这似乎既不是一个错误,也不是一个未记录的功能。

I just found the solution to actually "override" stubs / expectations:
Rhino Mocks: How to clear previous expectations on an object?

Applied to my problem I can now write:

this.Foo.Stub(f => f.DoSomething(42)).Return(43).Repeat.Any();

The documentation to Any says:

Repeat the method any number of times. This has special affects in that this method would now ignore orderring.

So this seems to be neither a bug or an undocumented feature.

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