1

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

aminator
  • 396
  • 7
  • 18
  • Problem is specific your your machine/code. Your example simply [works in both versions](https://godbolt.org/z/uGRhtp). Something bad is done in code which you didn't show. – Marek R Jul 31 '19 at 09:26
  • Also there are couple things which should be done in a [bit different way](https://godbolt.org/z/0mNXfE). – Marek R Jul 31 '19 at 09:30
  • I copy&pasted your code samples into my Source.cpp and Visual Studio will still tell me "Function Definition for 'MOCK_METHOD' not found." also there is nothing in the project except the Source.cpp and packages.config that I showed [link](https://ibb.co/YXyNcbZ) – aminator Jul 31 '19 at 09:50
  • Just to be clear: is this a build issue or just a warning shown by IDE? AFAIK Visual Studio IDE without ReSharper installed shows some false positive warnings when it sees some macros from gtest/gmock. – Marek R Jul 31 '19 at 10:07
  • I copy&pasted this code: [link](https://godbolt.org/z/Il-Fxc) and it gave me this complie output: `1>Source.cpp 1>C:\Users\Amin Abromand\source\repos\Project2\Project2\Source.cpp(14,25): error C2061: syntax error: identifier 'PenUp' 1>C:\Users\Amin Abromand\source\repos\Project2\Project2\Source.cpp(14,42): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>C:\Users\Amin Abromand\source\repos\Project2\Project2\Source.cpp(14,42): warning C4183: 'MOCK_METHOD': missing return type; assumed to be a member function returning 'int'` – aminator Jul 31 '19 at 10:16
  • I have just recognized, if you chose an x86 msvc compiler, the compiler explorer is not giving any output :( – aminator Jul 31 '19 at 13:21
  • 1
    Your gMock-for-dummies Link is outdated and broken. New link is http://google.github.io/googletest/gmock_for_dummies.html – Blindleistung Oct 13 '21 at 10:16
  • I have updated the link. – aminator Oct 13 '21 at 12:54

2 Answers2

3

Here is how to do it, the Windows way. You need to install the Windows cmake here https://cmake.org/download/. Because the cmake bundled in Cygwin is NOT compatible to Visual Studio, you need the cmake.exe for Windows and then run it from DOS terminal. I have cygwin for other things, but for all things Visual Studio, it needs to be done the Window's way in the DOS terminal. I am using Visual Studio 2019.

git clone https://github.com/google/googletest.git

Then open DOS terminal, granting PATH can find cmake.exe already. Goto where you cloned the googletest repo.

cd googletest
mkdir c:\opt
mkdir build
cd build
cmake -G "Visual Studio 16 2019" -DCMAKE_INSTALL_PREFIX=c:\\opt ..

It shall generate the usual googletest-distribution.sln Visual Studio solution file. It is now time to use the mouse. Double-click on this solution file, and it shall launch Visual Studio IDE. Build ALL_BUILD and INSTALL. The ALL_BUILD will produce *.lib files and the INSTALL will copy these files to c:\opt location. After this, the gtest/gmock lib and headers shall be located in C:\opt\ folder.

daparic
  • 3,794
  • 2
  • 36
  • 38
  • When I build googltest as you suggested - the resulting solution builds fine. But I need a `x86` version, so I added `-A Win32` option. In this way the resulting solution doesn't compile due to `gtest-port.h(2194,20): error C2039: 'int_t': is not a member of 'std'`. Any idea how to properly build a `x86 version` of googletest? – Viktor Be Jun 17 '21 at 19:29
1

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

aminator
  • 396
  • 7
  • 18