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

时间:2018-12-01 05:28:47

标签: c++ unit-testing opencv googletest gmock

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

问题:

我无法对要使用cv :: Mat并返回cv :: Mat的函数使用EXPECT_CALL方法。编译器说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 ++类方法的演示?

1 个答案:

答案 0 :(得分: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);
}