GMock:错误:无法将'cv :: MatExpr'转换为'bool'作为回报

问题描述 投票:0回答:1

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

问题:

我无法将EXPECT_CALL方法用于接受cv :: Mat并返回cv :: Mat的函数。编译器说gmock-matcher无法从cv :: MatExpr转换为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

以下是我的模拟类:

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中返回cv :: Mat的C ++类方法的演示?

c++ unit-testing opencv googletest gmock
1个回答
3
投票

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

但OpenCV宣称他们的比较为cv::MatExpr operator==(cv::Mat, cv::Mat)。它返回一个布尔矩阵而不是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);
}
© www.soinside.com 2019 - 2024. All rights reserved.