-2

I got the issue MockInterfaceTest.MockTest unknown file: error: C++ exception with description " The mock function has no default action set, and its return type has no default value set." thrown in the test body. Can anyone explain why this comes? I googled it but didn't find anything.

#include "pch.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "../../StaticLib1/StaticLib1/SysProps.h"


class MockTest : public PropertyInterface
{
public:
    MOCK_METHOD0(getComputerName, TCHAR*());
    MOCK_METHOD0(getSysDirectory, TCHAR*());
    MOCK_METHOD0(getUserName, TCHAR*());
    MOCK_METHOD0(getWindowsDir, TCHAR*());
    MOCK_METHOD0(getHardwareValue, SYSTEM_INFO());
};
using ::testing::_;
TEST(MockInterfaceTest, MockTest)
{
    MockTest mt;
    EXPECT_CALL(mt, getComputerName()).Times(1);
    EXPECT_CALL(mt, getSysDirectory()).Times(1);
    EXPECT_CALL(mt, getUserName()).Times(1);
    EXPECT_CALL(mt, getWindowsDir()).Times(1);
    EXPECT_CALL(mt, getHardwareValue()).Times(1);
    mt.getComputerName();
    mt.getSysDirectory();
    mt.getUserName();
    mt.getWindowsDir();
    mt.getHardwareValue();
}
int main(int argc, char** argv) {
    ::testing::InitGoogleMock(&argc, argv);
    return RUN_ALL_TESTS();
}
  • 1
    The error means "You didn't specify return values at `EXPECT_CALLS` and GMock doesn't know what to return by default". I suppose it doesn't know how to construct default `SYSTEM_INFO` (or it doesn't know that it should do so), but I never used WinApi and I'm not sure about that. Anyway, you can use [ON_CALL](https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#setting-the-default-actions-for-a-mock-method) to set default actions for mock to perform. This will likely solve the problem – Yksisarvinen Jan 24 '19 at 13:16
  • You can try to set default value for SYSTEM_INFO: ::testing::DefaultValue::Set(SYSTEM_INFO()); – Pavel K. Oct 29 '19 at 09:27

1 Answers1

0

You did not provide the signature of the functions you are mocking (the header file PropertyInterface and the type definition for SYSTEM_INFO would have been very useful).

Still, from the error message and the form of the MOCK declaration alone I guess that there is (for gtest) no useable default constructor for the SYSTEM_INFO type.

Since you did not specify what exactly the MOCK should return when getHardwareValue() (nor any other of the mocked functions) is called, it tries to return a default constructed value (as fallback) but since there is no way to create a "default" SYSTEM_INFO it throws an exception as a last resort.

CharonX
  • 2,130
  • 11
  • 33