Search code examples
c++visual-studiogoogletestvisual-studio-2019googlemock

gMock for Dummies - How to Getting Started? (Visual-Studio-2019)


I tried to create a very simple Mock Class as per Getting Started of gMock for Dummies.

  1. I created a new blank project in VS Studio 2019

  2. I ran the package manager Install-Package gmock -Version 1.8.1 as per this answer Configure GoogleMock

  3. The packages.config file that got created

    <?xml version="1.0" encoding="utf-8"?>`
    <packages>
        <package id="gmock" version="1.8.1" targetFramework="native" />
    </packages>
    
  4. I created 1 file Source.cpp

    class Turtle {
        virtual void PenUp();
    };
    
    void Turtle::PenUp() {
        return;
    }
    
    #include "gmock/gmock.h"
    
    class MockTurtle : public Turtle {
    public:
    
        MOCK_METHOD(void, PenUp, (), (override)); // not working see Pic1
    
        MOCK_METHOD0(PenUp, void()); // not working see Pic2
    
    };
    

The 2nd example tries a syntax like this answer How to mock method

Pic1:

Pic1 - MOCK_METHOD

Pic2:

Pic2 - MOCK_METHOD0


Solution

  • I was able to compile my solution now:

    1. I created a new blank project in VS Studio 2019
    2. I cloned the google repo googletest
    3. In the project properties I added C/C++ add. include directories

      path\to\repo\googlemock
      path\to\repo\googlemock\include
      path\to\repo\googlemock\include\gmock
      path\to\repo\googlemock\include\gmock\internal
      path\to\repo\googletest
      path\to\repo\googletest\include
      path\to\repo\googletest\include\gtest
      path\to\repo\googletest\include\gtest\internal
      
    4. I created 1 file Source.cpp

      #include "gmock/gmock.h"
      #include "gtest/gtest.h"
      
      #include "src/gmock-cardinalities.cc"
      #include "src/gmock-internal-utils.cc"
      #include "src/gmock-matchers.cc"
      #include "src/gmock-spec-builders.cc"
      #include "src/gmock.cc"
      
      #include "src/gtest.cc"
      #include "src/gtest-death-test.cc"
      #include "src/gtest-filepath.cc"
      #include "src/gtest-port.cc"
      #include "src/gtest-printers.cc"
      #include "src/gtest-test-part.cc"
      #include "src/gtest-typed-test.cc"
      
      class Turtle {
      public:
          virtual ~Turtle() {}
          virtual void PenUp() = 0;
      };
      
      class MockTurtle : public Turtle {
      public:
      
          //MOCK_METHOD0(PenUp, void()); // working =)
          MOCK_METHOD(void, PenUp, (), (override)); // working =)
      
      };
      
      int main(int, const char* []) {
          return 0;
      }
      

    Idea came from this blog post