Mocking Parametrized Constructor using Gmock

喜欢而已 提交于 2019-12-08 17:19:01

问题


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


回答1:


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));
};

or even

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

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


来源:https://stackoverflow.com/questions/17465371/mocking-parametrized-constructor-using-gmock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!