c++ - Google Mock to test the real behaviors of a class -
c++ - Google Mock to test the real behaviors of a class -
i new google test , google mock still little bit confused. tried implement simple calculator integer addition, multiplication , partition , create mock follows real behaviours. how can prepare or did wrong?
also can explain me how can sure mocking original class instead of straight calling original class? give thanks ahead.
here cbasicmath.hpp:
#ifndef basic_math_hpp__ #define basic_math_hpp__ class cbasicmath { public: cbasicmath(){} virtual ~cbasicmath() {} virtual int addition(int x, int y); virtual int multiply(int x, int y); virtual int divide(int x, int y); }; #endif //basic_math_hpp__
here cbasicmath.cpp:
#include "cbasicmath.hpp" int cbasicmath::addition(int x, int y) { homecoming (x + y); } int cbasicmath::multiply(int x, int y) { homecoming (x * y); } int cbasicmath::divide(int x, int y) { homecoming (x / y); }
here mock_basic_test.cpp:
#include "gmock/gmock.h" #include "cbasicmath.cpp" using ::testing::_; using ::testing::atleast; using ::testing::invoke; class mockbasictest : public cbasicmath { public: mockbasictest() { // default, calls delegated real object. on_call(*this, addition(_)) .willbydefault(invoke(&real_, &cbasicmath::addition)); on_call(*this, multiply(_)) .willbydefault(invoke(&real_, &cbasicmath::multiply)); on_call(*this, divide(_)) .willbydefault(invoke(&real_, &cbasicmath::divide)); } mock_method2(addition, int(int x, int y)); mock_method2(multiply, int(int x, int y)); mock_method2(divide, int(int x, int y)); private: cbasicmath real_; };
here testbasicmath:
#include "mock_basic_test.h" #include "gtest/gtest.h" #include "gmock/gmock.h" class basicmathtest : public ::testing::test { protected: basicmathtest() {} virtual ~basicmathtest() {} virtual void setup() { mtestobj = new cbasicmath(); } virtual void teardown() { delete mtestobj; } cbasicmath *mtestobj; }; test_f(basicmathtest, testaddition) { mockbasictest basictest; expect_call(basictest, addition(2,3)) .times(1); expect_eq(5,basictest.addition(2,3)); } test_f(basicmathtest, testmultiply) { expect_eq(6,mtestobj->multiply(2,3)); } test_f(basicmathtest, testdivide) { expect_eq(6,mtestobj->divide(6,1)); } int main(int argc, char **argv) { ::testing::initgoogletest(&argc, argv); homecoming run_all_tests(); }
it gives me errors next 3 functions:
in file included /home/gmock-1.7.0/include/gmock/gmock-generated-function-mockers.h:43:0, /home/gmock-1.7.0/include/gmock/gmock.h:61, mock_basic_test.h:1, testbasicmath_googletest.cpp:1: mock_basic_test.h: in constructor ‘mockbasictest::mockbasictest()’: mock_basic_test.h:12:30: error: no matching function phone call ‘mockbasictest::gmock_addition(const testing::internal::anythingmatcher&)’ on_call(*this, addition(_)) ^ mock_basic_test.h:12:30: note: candidate is: in file included /home/gmock-1.7.0/include/gmock/gmock.h:61:0, mock_basic_test.h:1, testbasicmath_googletest.cpp:1: mock_basic_test.h:19:2: note: testing::internal::mockspec<int(int, int)>&mockbasictest::gmock_addition(const testing::matcher<int>&, const testing::matcher<int>&) mock_method2(addition, int(int x, int y)); ^ mock_basic_test.h:19:2: note: candidate expects 2 arguments, 1 provided
thank 1 time again help.
in mocking class constructor code
on_call(*this, addition(_)) .willbydefault(invoke(&real_, &cbasicmath::addition));
the number of matchers (_
) need same number of parameters defined function signature:
on_call(*this, addition(_,_)) // ^^ add together additional matcher .willbydefault(invoke(&real_, &cbasicmath::addition)); // ... mock_method2(addition, int(int x, int y)); // ^^^^^ ^^^^^ here have 2 parameters need matching
c++ unit-testing googletest googlemock
Comments
Post a Comment