使用Gmock模拟参数化构造函数 [英] Mocking Parametrized Constructor using Gmock

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

问题描述

我有类被嘲笑,但它没有默认构造函数。我不能更改源代码。所以有什么办法使用Gmock来模拟参数化的构造函数。

I have class to be mocked but it is not having default constructor. I cannot change the source code. So is there any way to mock a parametrized constructor using Gmock

推荐答案

只需让你的Mock的构造函数调用带有正确参数的模拟类的构造函数:

Yes there is. Just let your Mock's constructor call the mocked class' constructor with the right arguments:

class base_class {
public:
    base_class(int, int) {}

    virtual int foo(int);
};


class base_mock : public base_class {
public:
    base_mock() : base_class(23, 42) {}

    MOCK_METHOD1(foo, int(int));
};

或甚至

class base_mock : public base_class {
public:
    base_mock(int a, int b) : base_class(a, b) {}

    MOCK_METHOD1(foo, int(int));
};

这篇关于使用Gmock模拟参数化构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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