调用模拟方法的预期方法时调用方法 [英] Calling a method when expected method on mock was invoked

查看:116
本文介绍了调用模拟方法的预期方法时调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:


class InterfaceA;
class InterfaceB;
class InterfaceC;


class InterfaceA
{
  virtual void foo(InterfaceC&) = 0;
};

class InterfaceB
{
  virtual void bar() = 0;
};

class InterfaceC
{
  virtual void bla() = 0;
};

// MOCKs

class MockA : public InterfaceA
{
  public:
    MOCK_METHOD0(foo, void(InterfaceC&));
};

class MockB : public InterfaceB
{
  public:
    MOCK_METHOD0(bar, void());
};


class ImplC : public InterfaceC
{
  public:
    ImplC(InterfaceA& a, Interface& b) : m_a(a), m_b(b) {}

    void doSomething() {
      m_a.foo(*this);
    }

    virtual void bla()
    {
      m_b.bar();
    }
};


MockA mockA;
MockB mockB;

EXPECT_CALL(mockA, foo());

ImplC impl(mockA, mockB);

impl.doSomething(); // will call foo on mockA

如果调用doSomething,则将在MockA上调用foo.万一将调用foo,我如何触发方法bla的调用?是否可以通过某种方式创建期望:

In case doSomething is invoked, foo will be called on MockA. How can I trigger an invocation of the method bla, in case foo will be invoked? Is it somehow possible to create an expectation like:


EXPECT_CALL(mockA, foo()).WillOnce(Invoke(impl.bla()));

?

我希望答案是明确的,例子也是这样.

I hope the answer is clear and the example too.

先谢谢了. 玛特

推荐答案

EXPECT_CALL(mockA, foo()).WillOnce(InvokeWithoutArgs(&impl, &ImplC::bla));

应该工作.如果必须传递更复杂的参数,请使用boost :: bind(请注意,类实例和方法在参数列表中的顺序不同):

Should work. If you have to pass more complex parameters, use boost::bind (notice the different order of the class instance and method in the parameter list):

EXPECT_CALL(mockA, foo())
    .WillOnce(Invoke(boost::bind(&ImplC::bla, &impl, other_params)));

如果给了foo()一些应传递给bla()的参数,请使用WithArgs:

And if foo() is given some parameters that should be passed into bla(), use WithArgs:

EXPECT_CALL(mockA, foo(Lt(1), _))
    .WillOnce(WithArgs<0>(Invoke(&impl, &ImplC::bla)));

还可以看看Google Mock 备忘单Wiki页面-它提供有关函数和方法调用操作的更多信息.

Also take a look at the Google Mock Cheat Sheet wiki page - it provides more information about function- and method calling actions.

这篇关于调用模拟方法的预期方法时调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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