Mocking parameterized constructor using Gmock

I have a class that will mock, but it does not have a default constructor. I can not change the source code. So, is there a way to mock a parameterized constructor using Gmock

+7
source share
1 answer

Yes there is. Just let your Mock constructor invoke the mock class constructor with the correct 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)); }; 
+20
source

Source: https://habr.com/ru/post/948687/


All Articles