使用RhinoMocks,我如何断言调用了几种方法之一? [英] Using RhinoMocks, how can I assert that one of several methods was called?

查看:111
本文介绍了使用RhinoMocks,我如何断言调用了几种方法之一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下服务接口:

public interface IServiceA
{
    void DoSomething(string s);
    void DoSomething(string s, bool b);
}

public interface IServiceB
{
    void DoSomething();
}

IServiceB的实现取决于IServiceA,如下所示:

The implementation of IServiceB depends on IServiceA like this:

public class ServiceB : IServiceB
{
    private IServiceA _serviceA;

    public ServiceB(IServiceA serviceA)
    {
        _serviceA = serviceA;
    }

    public void DoSomething()
    {
        _serviceA.DoSomething("Hello", true);
    }
}

即.依赖项被注入到构造函数中.

Ie. the dependency is injected in the constructor.

现在考虑对DoSomething()方法进行单元测试.我想断言,在IServiceA中调用了一个重载的DoSomething方法,但是遵循一个普遍的原则,即单元测试对被测试方法的内部工作不应该了解太多,我希望与哪个方法无关.调用了两个重载.考虑以下单元测试:

Now consider a unit test for the DoSomething() method. I wish to assert that one of the overloaded DoSomething-methods in IServiceA is called, but following a general principle that unit tests shouldn't know too much about the internal workings of the method being tested, I wish to be agnostic about which of the two overloads is called. Consider the following unit test:

[TestFixture]
public class ServiceBTests
{
    [Test]
    public void DoSomething_CallsServiceA()
    {
        var serviceAMock = MockRepository.GenerateMock<IServiceA>();
        var service = new ServiceB(serviceAMock);

        service.DoSomething();

        // Problem: How to check if EITHER:
        serviceAMock.AssertWasCalled(s => s.DoSomething(Arg<String>.Is.NotNull, Arg<bool>.Is.Anything));
        // OR:
        serviceAMock.AssertWasCalled(s => s.DoSomething(Arg<String>.Is.NotNull));
    }
}

我怎么能断言这两种方法中的 或另一种方法都被调用了?

How can I assert that either one or the other of the two methods was called?

推荐答案

您可以像这样手动设置布尔标志:

You could manually set a boolean flag like so:

[TestFixture]
public class ServiceBTests
{
    [Test]
    public void DoSomething_CallsServiceA()
    {
        var serviceAMock = MockRepository.GenerateMock<IServiceA>();
        bool called = false;
        serviceAMock.Stub(
             x => x.DoSomething(Arg<String>.Is.NotNull, Arg<bool>.Is.Anything))
            .WhenCalled(delegate { called = true; });
        serviceAMock.Stub(x => x.DoSomething(Arg<String>.Is.NotNull))
            .WhenCalled(delegate { called = true; });

        var service = new ServiceB(serviceAMock);

        service.DoSomething();

        Assert.IsTrue(called);
    }
}

我认为这不是很有用.单元测试涉及在组件边界之外可以观察到的任何事物.对模拟的方法调用是其中的一部分.换句话说,如果您测试是否正在调用特定的重载,就可以了.毕竟,一定有理由为什么要使用该重载,而不要使用另一个重载.

I don't think this very useful though. Unit tests are concerned with anything that is observable from outside of the component boundaries. Method calls to mocks are part of that. In other words, it is OK if you test for a specific overload being called. After all, there must be a reason why you use that overload and not the other one.

如果您确实希望单元测试对实现一无所知,则根本不允许您对模拟进行断言方法调用.那将严重限制您编写测试的能力.

If you really want the unit test to remain ignorant of the implementation, you wouldn't be allowed to assert method calls on mocks at all. That would be a severe restriction on your ability to write tests.

这篇关于使用RhinoMocks,我如何断言调用了几种方法之一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆