使用Array作为参数的预期方法调用

时间:2015-11-17 21:28:32

标签: c++ gmock

对于以下模拟:

class MockUI8CBuff : public UI8CBuff_I {
 public:
    MOCK_METHOD2(put, unsigned(const uint8_t* start, unsigned n));
};

以下测试片段

MockUI8CBuff BFO;

uint8_t arr[] = {1, 2, 3};

EXPECT_CALL(BFO, put(ElementsAreArray(arr, 3), 2))
    .WillOnce(Return(1));

我收到以下错误

/home/sporty/ws-ccs/googletest/googlemock/include/gmock/gmock-matchers.h|3114 col 20| error: type 'StlContainer' (aka 'const unsigned char *') cannot be used prior to '::' because it has no members
||   typedef typename StlContainer::value_type Element;
||                    ^
/home/sporty/ws-ccs/googletest/googlemock/include/gmock/gmock-matchers.h|3532 col 28| note: in instantiation of template class 'testing::internal::ElementsAreMatcherImpl<const unsigned char *>' requested here
||     return MakeMatcher(new ElementsAreMatcherImpl<Container>(
||                            ^
miwt-os/coap/unittest/cbor_encoder_test.cpp|126 col 26| note: in instantiation of function template specialization 'testing::internal::ElementsAreArrayMatcher<unsigned char>::operator Matcher<const unsigned char *>' requested here
||     EXPECT_CALL(BFO, put(::testing::ElementsAreArray(arr, 3), 2))
||                          ^
/home/sporty/ws-ccs/googletest/googlemock/include/gmock/gmock-spec-builders.h|1845 col 61| note: expanded from macro 'EXPECT_CALL'
|| #define EXPECT_CALL(obj, call) GMOCK_EXPECT_CALL_IMPL_(obj, call)
||                                                             ^
/home/sporty/ws-ccs/googletest/googlemock/include/gmock/gmock-spec-builders.h|1844 col 20| note: expanded from macro 'GMOCK_EXPECT_CALL_IMPL_'
||     ((obj).gmock_##call).InternalExpectedAt(__FILE__, __LINE__, #obj, #call)
||                    ^
|| 1 error generated.
miwt-os/coap/unittest/cbor_encoder.makefile|109| recipe for target 'cbor_encoder_test.o' failed
|| make: *** [cbor_encoder_test.o] Error 1

为什么会出现此错误?以下作品:

EXPECT_CALL(BFO, put(Pointee(1), 2))
    .WillOnce(Return(1));

1 个答案:

答案 0 :(得分:1)

无法将指针与数组进行比较。您应该使用如下语法:

EXPECT_CALL(BFO, put(_, _))
    .With(Args<0,1>(ElementsAre(1, 2, 3)));   // ElementsAreArray can also be used