-2

I am having trouble in mocking the following method.

I have this code:


// contents of XRECT.h
using VRECT = std::vector<RECT>;

class XRECT {
public:
    virtual bool DisplayRect(VRECT rects) = 0;
}

class CXRECT : public XRECT {
public:
    virtual bool DisplayRect(VRECT rects) override;
}

// contents of XRECT.cpp
bool CXRECT::DisplayRect(VRECT rects) {
    // do something
    return true;
}

// contents of test.cpp
#include "gtest/gtest.h"
#include "gmock/gmock.h"

#include "XRECT.h"

class MockCXRECT : public CXRECT {
public:
    MOCK_METHOD(bool, DisplayRect, (VRECT rects), (override)); // hovering over the mock method shows the "Function definition for MOCK_METHOD" not found
};

TEST(Testing, Case1) {
    MockCXRECT mock;
    VRECT v;
    EXPECT_CALL(mock, DisplayRect(v)).WillRepeatedly(::testing::Return(true));
}

When I replace VRECT with a simple data type int then everything works just fine. Is there a different way to define container based arguments?

I am getting error for compiling: error C2893: Failed to specialize function template 'unknown-type std::equal_to<void>::operator ()(_Ty1 &&,_Ty2) noexcept(<expr>) const'

++++ Edit: I see the problem is not with vector but with windef.h's RECT.

kishoredbn
  • 2,007
  • 4
  • 28
  • 47
  • You haven't said what goes wrong when using a vector. After fixing some minor compilation issues (missing `#include ` and `;` at the end of class definitions), the code compiles just fine: https://godbolt.org/z/5vE6TvMYs – Kevin Dec 23 '22 at 14:07
  • [tag:gmock] is for Groovy language. Read it carefully. Try to find a better tag for Google mock. – 273K Dec 23 '22 at 15:12
  • @Kevin can you recheck. I am getting error in compiling. error C2893: Failed to specialize function template 'unknown-type std::equal_to::operator ()(_Ty1 &&,_Ty2) noexcept() const' – kishoredbn Dec 23 '22 at 16:15
  • @Kevin Stackoverflow crashed when I tried editing my first version, and was offline for hours. – kishoredbn Dec 23 '22 at 16:16

1 Answers1

0

So I solved the problem, by adding a template overloading.

// contents of XRECT.h
using VRECT = std::vector<RECT>;

class XRECT {
public:
    virtual bool DisplayRect(VRECT rects) = 0;
}

class CXRECT : public XRECT {
public:
    virtual bool DisplayRect(VRECT rects) override;
}

// contents of XRECT.cpp
bool CXRECT::DisplayRect(VRECT rects) {
    // do something
    return true;
}

// contents of test.cpp
#include "gtest/gtest.h"
#include "gmock/gmock.h"

#include "XRECT.h"

class MockCXRECT : public CXRECT {
public:
    MOCK_METHOD(bool, DisplayRect, (VRECT rects), (override)); // hovering over the mock method shows the "Function definition for MOCK_METHOD" not found
};

TEST(Testing, Case1) {
    MockCXRECT mock;
    VRECT v;
    EXPECT_CALL(mock, DisplayRect(v)).WillRepeatedly(::testing::Return(true));
}

Hope it helps someone who stumbles with same type of errors. Credit goes to: Google Test can't find user provided equality operator

kishoredbn
  • 2,007
  • 4
  • 28
  • 47