如何对重载的运算符使用gmock MOCK_METHOD? [英] How to use gmock MOCK_METHOD for overloaded operators?

查看:143
本文介绍了如何对重载的运算符使用gmock MOCK_METHOD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是googlemock(和StackOverflow)的新手.在googlemock中使用MOCK_METHODn时遇到问题,我相信此功能已被广泛使用.这就是我所做的.

I am new to googlemock (and StackOverflow). I got a problem when using MOCK_METHODn in googlemock and I believe this function is widely used. Here is what I did.

我有一个抽象类Foo,其中虚拟重载了operator[]:

I have an abstract class Foo with virtual overloaded operator[]:

class Foo{
public:
      virtual ~Foo(){};
      virtual int operator [] (int index) = 0;
}

,我想用googlemock来获取MockFoo:

and I want to use googlemock to get a MockFoo:

class MockFoo: public Foo{
public:
      MOCK_METHOD1(operator[], int(int index));  //The compiler indicates this line is incorrect
}

但是,这段代码给了我这样的编译错误:

However, this code gives me a compile error like this:

error: pasting "]" and "_" does not give a valid preprocessing token
  MOCK_METHOD1(operator[], GeneInterface&(int index));

我的理解是编译器会误解operator[],并且不会将其用作方法名称.但是,使用MOCK_METHODn模拟operator[]的正确方法是什么?我已阅读github> docs上的

My understanding is that compiler misunderstands the operator[] and doesn't take it as a method name. But what is the correct way to mock the operator[] by using MOCK_METHODn? I have read the docs from googlemock but found nothing related to my question. Cound anyone help me with it?

谢谢!

推荐答案

您不能.请参阅: https://groups.google.com/forum/#!topic /googlemock/O-5cTVVtswE

解决方案是仅创建一个常规的老式重载方法,如下所示:

The solution is to just create a regular old fashioned overloaded method like so:

class Foo {
 public:
 virtual ~Foo() {}
 virtual int operator [] (int index) = 0;
};

class MockFoo: public Foo {
 public:
 MOCK_METHOD1(BracketOp, int(int index));
 virtual int operator [] (int index) override { return BracketOp(index); }
}; 

这篇关于如何对重载的运算符使用gmock MOCK_METHOD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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