c++ - Gmock - how to override library function -
i trying override simple cmath library function - 'pow'
below code :
#include "stdafx.h" #include <gmock/gmock.h> #include <gtest/gtest.h> #include <cmath> class powinterface { public: virtual ~powinterface() {} virtual double pow(double _xx, int _yx) = 0; }; class libxmock : public powinterface { public: virtual ~libxmock() {} mock_method2(pow, double(double _xx, int _yx)); }; int main() { libxmock libxmockobj; expect_call(libxmockobj, pow(10.00, 3)); double ans = libxmockobj.pow(10.00, 3); expect_eq(2, ans); }
the code compiles fine execution gives below output:
c:\gmock> _test_example.cpp(36): error: expected: 2 equal to: ans is: 0 press key continue . . .
i unsure why returned value 0 - wondered if need add self definition of 'pow' method injected below code:
double pow(double _xx, int _yx) { return libxmockobj.pow(_xx , _yx); }
now compiler fails compile code stating re-definition of pow method - hence how can implement own pow library method , call it?
i did not try compile, idea that:
#include "stdafx.h" #include <gmock/gmock.h> #include <gtest/gtest.h> #include <cmath> class powinterface { public: virtual ~powinterface() {} virtual double pow(double _xx, int _yx) = 0; }; class libxmock : public powinterface { public: virtual ~libxmock() {} mock_method2(pow, double(double _xx, int _yx)); double custompow(double _xx, int _yx) { return 2; } }; int main() { libxmock libxmockobj; expect_call(libxmockobj, pow(10.00, 3)).times(1).willonce(::testing::invoke(libxmockobj, &libxmock::custompow)); double ans = libxmockobj.pow(10.00, 3); expect_eq(2, ans); }
Comments
Post a Comment