samedi 6 avril 2019

Google Mock:Testing a Certain Property of an Object

I try to define a custom matcher following the example that is provided in Google Mock CookBook.

The code is the following

    #include "gmock/gmock.h"
    #include "gtest/gtest.h"
    using namespace std;
    using ::testing::MatcherInterface;
    using ::testing::MatchResultListener;
    using ::testing::Matcher;
    using ::testing::_;
    using ::testing::AtLeast;
    using ::testing::Invoke;
    class Foo2;
    class Foo2
    {
        public:
            virtual int bar() const
            {
                return 4;
            }
            virtual int baz() const
            {
                return 5;
            }
            virtual void DoThis (Matcher<const Foo2&> pFunc)
          {
                     std::cout << "Foo:DoThis" << std::endl;
          }
          virtual void DoThat(int)
          {
                 std::cout << "Foo:DoThat" << std::endl;
          }
            virtual ~Foo2()
            {

            }

    };

    class BarPlusBazEqMatcher : public MatcherInterface<const Foo2&> {
     public:
      explicit BarPlusBazEqMatcher(int expected_sum)
          : expected_sum_(expected_sum) {}

      virtual bool MatchAndExplain(const Foo2& foo,
                                   MatchResultListener* listener) const {
        return (foo.bar() + foo.baz()) == expected_sum_;
      }

      virtual void DescribeTo(::std::ostream* os) const {
        *os << "bar() + baz() equals " << expected_sum_;
      }

      virtual void DescribeNegationTo(::std::ostream* os) const {
        *os << "bar() + baz() does not equal " << expected_sum_;
      }
      virtual ~BarPlusBazEqMatcher()
      {

      }

     private:
      const int expected_sum_;
    };


    inline Matcher<const Foo2&> BarPlusBazEq(int expected_sum) {
      return MakeMatcher(new BarPlusBazEqMatcher(expected_sum));
    }


    class MockFoo2 : public Foo2 {
     public:

      MOCK_METHOD1(DoThis,void(Matcher<const Foo2&>));
      MOCK_METHOD1(DoThat, void(int));
    };

    TEST(MockMatcher, Matcher)
    {
        MockFoo2 mockF;
        EXPECT_CALL(mockF, DoThis(BarPlusBazEq(5)));
    }

When I try to compile the aforementioned code, but the following compile errors are generated

....gtest\gtest.h:9160:60: error: no match for 'operator==' (operand types are 'const testing::Matcher' and 'const testing::Matcher') bool operator()(const A& a, const B& b) const { return a == b; } ~~^~~~

....gtest\gtest.h:14096:13: note: candidate: bool testing::internal::operator==(testing::internal::faketype, testing::internal::faketype) inline bool operator==(faketype, faketype) { return true; }

....gtest\gtest.h:14096:13: note: no known conversion for argument 1 from 'const testing::Matcher' to 'testing::internal::faketype'

How do I solve these errors?

Thank you

Aucun commentaire:

Enregistrer un commentaire