GMock : error: cannot convert ‘cv::MatExpr’ to ‘bool’ in return

GMock : error: cannot convert ‘cv::MatExpr’ to ‘bool’ in return

上下文: 我正在尝试使用 GMock 模拟 OpenCV-C++ class。

问题:

我无法对接受 cv::Mat 和 return 的函数使用 EXPECT_CALL 方法 cv::Mat。编译器说 gmock-matcher 无法从 cv::MatExpr 转换为 return.

中的 bool

以下是编译时的详细错误信息。

In file included from /home/arun/Documents      /LaneDetection/test/../vendor/googletest/googlemock/include/gmock/gmock-spec-builders.h:75:0,
             from /home/arun/Documents/LaneDetection/test/../vendor/googletest/googlemock/include/gmock/gmock-generated-function-mockers.h:43,
             from /home/arun/Documents/LaneDetection/test/../vendor/googletest/googlemock/include/gmock/gmock.h:61,
             from /home/arun/Documents/LaneDetection/test/test.cpp:32:
/home/arun/Documents/LaneDetection/test/../vendor    /googletest/googlemock/include/gmock/gmock-matchers.h: In instantiation of ‘bool     
testing::internal::AnyEq::operator()(const A&, const B&) const [with A = cv::Mat; B = cv::Mat]’:
/home/arun/Documents/LaneDetection/test/../vendor/googletest/googlemock/include/gmock/gmock-matchers.h:908:18:   
required from ‘bool testing::internal::ComparisonBase<D, Rhs, Op>::Impl<Lhs>::MatchAndExplain(Lhs, testing::MatchResultListener*) const [with Lhs = cv::Mat; D = testing::internal::EqMatcher<cv::Mat>; Rhs = cv::Mat; Op = testing::internal::AnyEq]’
/home/arun/Documents/LaneDetection/test/test.cpp:77:39:   required from here
/home/arun/Documents/LaneDetection/test/../vendor/googletest/googlemock/include/gmock/gmock-matchers.h:204:63: error: cannot convert ‘cv::MatExpr’ to ‘bool’ in return
   bool operator()(const A& a, const B& b) const { return a == b; }
                                                               ^
test/CMakeFiles/cpp-test.dir/build.make:86: recipe for target 'test/CMakeFiles/cpp-test.dir/test.cpp.o' failed
make[2]: *** [test/CMakeFiles/cpp-test.dir/test.cpp.o] Error 1
CMakeFiles/Makefile2:178: recipe for target 'test/CMakeFiles/cpp-test.dir/all' failed
make[1]: *** [test/CMakeFiles/cpp-test.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2

以下是我的嘲讽class:

Thresholder.hpp

class Thresholder {
 public:

    Thresholder() {}
    virtual ~Thresholder() {} 
    virtual cv::Mat convertToLab(cv::Mat smoothImg);

 private:
    cv::Mat inputImg;   // < Container used for storing input image
    cv::Mat labImage;   // < Container for LAB converted input image
};

Thresholder.cpp

cv::Mat Thresholder::convertToLab(cv::Mat smoothImg) {
    inputImg = smoothImg;
    cv::cvtColor(inputImg, labImage, cv::COLOR_BGR2Lab);
    return labImage;
}

test.cpp

class MockThresholder : public Thresholder {
public:
    MOCK_METHOD1(convertToLab, cv::Mat(cv::Mat smoothImg));
};

/*
*@brief  : Creating test cases for mock class
*/

TEST(MockTest, ThreshTest) {
MockThresholder ThreshMock;
cv::Mat dummyXY = cv::Mat::ones(100, 100, CV_8UC3);
EXPECT_CALL(ThreshMock, convertToLab(dummyXY))
.Times(1)
.WillOnce(::testing::Return(dummyXY));}

问题:

似乎没有在线资源可以演示如何通过 gmock 有效地使用 OpenCV 和 C++。是否可以分享一些关于如何在 GMock 中测试 returns cv::Mat 的 C++ class 方法的演示?

这里的问题不是返回类型,而是预期的调用。具体来说 EXPECT_CALL(ThreshMock, convertToLab(dummyXY)) 让 GMock 检查调用的参数是否确实等于 dummyXY。默认情况下,它使用 == 比较运算符。

但是 OpenCV 将它们的比较声明为 cv::MatExpr operator==(cv::Mat, cv::Mat)。它 returns 一个布尔矩阵而不是 bool.

因此,您必须告诉 GMock 如何将您的预期调用与 a custom matcher 匹配。 您使用 MATCHER_... 宏创建匹配器:

MATCHER_P(cvMatMatches, expected, "Match arg cvMat to be equal to expected") {
  if (arg.size() != expected.size()) {
    return false;
  }
  auto differingElems = (arg != expected);
  return cv::countNonZero(differingElems) == 0;
}

你的测试代码变成:

TEST(MockTest, ThreshTest) {
  MockThresholder ThreshMock;
  cv::Mat dummyXY = cv::Mat::ones(100, 100, CV_8U);
  EXPECT_CALL(ThreshMock, convertToLab(cvMatMatches(dummyXY)))
      .Times(1)
      .WillOnce(::testing::Return(dummyXY));

  ThreshMock.convertToLab(dummyXY);
}