c++ - GMock - returning default value with ON_CALL for overloaded methods -


i'm trying write mock class contains 3 overloaded methods, ie.:

#include <gtest/gtest.h> #include <gmock/gmock.h>  using ::testing::_; using ::testing::return; using ::testing::a; using ::testing::byref; using ::testing::ref; using ::testing::typedeq;  struct foo {   int foomethod(const int& intparam) { return 0; }   int foomethod(const float& floatparam) { return 0; }   int foomethod(const std::string& stringparam) { return 0; } };  struct foomock {   foomock() {     on_call(*this, foomethod(_)).willbydefault(return(-1));   }    mock_method1(foomethod, int(const int& intparam));   mock_method1(foomethod, int(const float& floatparam));   mock_method1(foomethod, int(const std::string& stringparam)); }; 

but gives error:

 error: call of overloaded ‘gmock_foomethod(const testing::internal::anythingmatcher&)’ ambiguous 

i've tried typedeq() instead of "_", gives more obscure errors. i've checked gmock faq, wiki , didn't find solution - how can return default value on_call overloaded methods?

br, lukasz

@tx34 has crux of answer, there few more issues in code.

firstly, docs on selecting between overloaded functions appropriate. have 3 overloads of foomethod same number of arguments different argument types. you're going have use matcher specifies type.

next, need define foo functions mocked virtual, or else invoking them through foo object won't call derived mock functions. since you're defining foo base class, should have virtual destructor avoid slicing.

finally, need have foomock inherit foo.

so putting together, end like:

#include <memory> #include <string> #include "gtest/gtest.h" #include "gmock/gmock.h"  using ::testing::_; using ::testing::an; using ::testing::matcher; using ::testing::typedeq; using ::testing::return;  struct foo {   virtual ~foo() {}   virtual int foomethod(const int&) { return 0; }   virtual int foomethod(const float&) { return 0; }   virtual int foomethod(const std::string&) { return 0; } };  struct foomock : foo {   foomock() : foo() {     on_call(*this, foomethod(an<const int&>())).         willbydefault(return(-1));     on_call(*this, foomethod(matcher<const float&>(_))).         willbydefault(return(-2));     on_call(*this, foomethod(typedeq<const std::string&>("1"))).         willbydefault(return(-3));   }    mock_method1(foomethod, int(const int& intparam));   mock_method1(foomethod, int(const float& floatparam));   mock_method1(foomethod, int(const std::string& stringparam)); };  test(foo, foo) {   std::shared_ptr<foo> foo(new foomock);   auto foo_mock(std::dynamic_pointer_cast<foomock>(foo));    expect_call(*foo_mock, foomethod(matcher<const int&>(_))).times(1);   expect_call(*foo_mock, foomethod(matcher<const float&>(_))).times(1);   expect_call(*foo_mock, foomethod(matcher<const std::string&>(_))).times(1);    expect_eq(-1, foo->foomethod(1));   expect_eq(-2, foo->foomethod(1.0f));   expect_eq(-3, foo->foomethod("1")); }   int main(int argc, char **argv) {   testing::initgoogletest(&argc, argv);   return run_all_tests(); } 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -