Google模拟ByRef方法 [英] Google mock ByRef method

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

问题描述

我有一个将布尔值作为参考参数并返回整数的类:

I have a class that takes a boolean as a reference parameter and returns an integer:

class Foo
{
  public:
    Bar my_bar;
    virtual int myMethod(bool &my_boolean) = 0;
}

/*...*/

int Foo::myMethod(bool &my_boolean){
  if (my_bar == NULL){
    my_boolean = false;
    return -1;
  }
  else{
    my_boolean = true;
    return 0;
  }

}

我为此课程创建了一个模拟游戏:

And I created a mock for this class:

class MockFoo : public Foo
{
   MOCK_METHOD1(myMethod,int(bool &my_boolean));
}

我在如何设置对此类功能的期望方面遇到问题,因为我需要将返回值和参考参数设置为特定值以正确创建我的单元测试.我可以用gmock处理这种​​功能吗?我尝试按照我认为是文档中的解决方案的方法进行操作:

I'm having problems on how to set the expectations for this kind of function,because I need to set the return value and the reference parameter to specific values to properly create my unit tests.How can I deal with this kind of function with gmock?I tried following what I thought was the solution on the documentation:

using ::testing::SetArgPointee;

class MockMutator : public Mutator {
  public:
  MOCK_METHOD2(Mutate, void(bool mutate, int* value));
  ...
};
  ...

MockMutator mutator;
EXPECT_CALL(mutator, Mutate(true, _))
  .WillOnce(SetArgPointee<1>(5));

但是我要么不理解该示例,要么不适用于此案例.以前有没有人处理过这种情况?

But either I didn't understood the example or it wasn't applicable for this case.Has anyone dealt whith this kind of situation before?

谢谢.

推荐答案

您的问题很难得到!谷歌模拟烹饪书"中的示例也是如此.

Your question is hard to get! The samples from google mocks 'cook book' are so as well.

您是否要在模拟类中重用Foo::myMethod()的实现,还是只想模拟特定调用情况的副作用(返回值并由ref参数更改)?

Do you want to reuse the implementation for Foo::myMethod() with you mock class, or do you just want to mock the side effects (return value and changed by ref parameters) for specific call situations?

模拟类通常是用来替换/模拟您的Foo类,而不是直接继承它或它的行为.不知道您为纯方法定义此默认"行为的方式是否可行,但对此表示怀疑.然后,您可以简单地省略= 0. 更好的方法是分离出一个真实的接口声明,例如:

A mock class is usually meant to replace / simulate your Foo class, not to inherit it directly or it's behavior. Don't know if the way you define this 'default' behavior for a pure method will work, but doubt that. You might simply omit the = 0 then. The better approach would be to separate out a real interface declaration like:

struct IFoo
{
    virtual int myMethod(bool &my_boolean) = 0;
    virtual ~IFoo() {}
};

class Foo : public IFoo
{
    // ...
};

class MockFoo : public IFoo
{
   MOCK_METHOD1(myMethod,int(bool &my_boolean));
};

如果是后一种情况,则应该使用testing::Return(value)testing::SetArgReferee<N>(value)下车(在非常有用的).

If you have the latter case, you should get off with testing::Return(value) and testing::SetArgReferee<N>(value) (found that in the very useful 'Cheat Sheet').

您的期望电话应如下所示:

Your expectation call should look like this then:

MockFoo foo;

// Expect call to myMethod() return -1 and set the by ref argument to true
EXPECT_CALL(foo, myMethod(_))
  .WillOnce(DoAll(SetArgReferee<0>(true),Return(-1)));

// Expect call to myMethod() return 0 and set the by ref argument to false
EXPECT_CALL(foo, myMethod(_))
  .WillOnce(DoAll(SetArgReferee<0>(false),Return(0)));

如果您真的想为myMethod()重用原始的类逻辑,请查看分别委托给父类的委托" . '委派调用真实对象"

If you really want to reuse your original classes logic for myMethod() have a look at 'Delegating calls to a parent class', resp. 'Delegating Calls to a Real Object'

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

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