Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

gMock Visual Studio test crashes when using EXPECT_CALL

When running my test from the Test Explorer in Visual Studio 2022 [17.2.0], testhost.exe crashes when my unit test has a gMock EXPECT_CALL call in it. What am I doing wrong?

Minimal code:

#include <CppUnitTest.h>
#include <CppUnitTestAssert.h>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using ::testing::Return;

class MockCow
{
public:
    MOCK_METHOD0(Moo, int());
};

TEST_CLASS(the_unit_test)
{
    TEST_METHOD(the_test_method)
    {
        MockCow cow;

        EXPECT_CALL(cow, Moo())
            .WillOnce(Return(42));

        cow.Moo();
    }
};

If the EXPECT_CALL call is commented out, the unit test will not crash.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I created this project by making an Empty C++ project, then installing the NuGet gMock package, latest stable version 1.11.0, and adding library directories/dependencies in the project settings. The project configuration type is Dynamic Library (.dll).

For C/C++ Additional Include Directories, I have:
$(VCInstallDir)Auxiliary\VS\UnitTest\include;

For Linker Additional Library Directories, I have:
$(VCInstallDir)Auxiliary\VS\UnitTest\lib;

For Linker Input Additional Dependencies, I have:
x64\Microsoft.VisualStudio.TestTools.CppUnitTestFramework.lib

It compiles and links successfully, so the above is just for completeness. I have also tried installing previous versions of the gMock NuGet behavior, but I get the same behavior.

Faulting module name: MyTest.dll_unloaded, version: 0.0.0.0, time stamp: 0x62cd8255
Exception code: 0xc0000005
Fault offset: 0x000000000011e8f7
Faulting process id: 0x8b04
Faulting application start time: 0x01d895fa0d052ac3
Faulting application path: C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\Extensions\TestPlatform\testhost.exe

There are other posts about gMock crashes that may be tangentially related, but they either don’t have solutions or are missing details and were never updated.

>Solution :

  1. You mix the very different test frameworks.
  2. The more important thing is that you do not initialize Google test framework.
  3. TEST_CLASS, TEST_METHOD are of another test framework, do not perform required pre and post stuff, Google mock is not supposed to work there.

Choose one framework and use it.

#include <gmock/gmock.h>
#include <gtest/gtest.h

using ::testing::Return;

class MockCow
{
public:
    MOCK_METHOD0(Moo, int());
};

TEST(the_unit_test, the_test_method) {
    MockCow cow;

    EXPECT_CALL(cow, Moo())
        .WillOnce(Return(42));

    cow.Moo();
};

int main(int argc, char *argv[]) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading