如何在GMOCK测试中使用Json :: Value类对象

时间:2018-03-27 06:43:00

标签: c++ googletest gmock jsoncpp

class LoadLibTaskImpl {
        public:
                LoadLibTaskImpl() { }
                virtual ~LoadLibTaskImpl() { }
                virtual inline SharedLibrary *load_mal_lib(string, string, Json::Value &) = 0;
};

class LoadLibTask: public LoadLibTaskImpl {
        public:
                LoadLibTask() : LoadLibTaskImpl() {}
                ~LoadLibTask() {}
                inline SharedLibrary *load_library(string cm_lib_path, string lib_name) {
                        lib_name = cm_lib_path + "/" + lib_name;
                        SharedLibrary *load_library = NULL;
                        load_library = new SharedLibrary(lib_name, Poco::SharedLibrary::Flags::SHLIB_GLOBAL);
                        return load_library;
                }

                inline SharedLibrary *load_mal_lib(string cm_path, string lib_name, Json::Value &binaries_info) {
                        SharedLibrary *lib = load_library(cm_path, lib_name);
                        cm_binary_info func = (cm_binary_info) lib->getSymbol("binary_info");
                        binaries_info[lib_name] = func();
                        return lib;
                }
}

class MockLoadLibTask: public LoadLibTask{
        public:
                MockLoadLibTask() : LoadLibTask() {}
                ~MockLoadLibTask() {}
                MOCK_METHOD2(load_library, SharedLibrary *(string cm_lib_path, string lib_name));
                MOCK_METHOD3(load_mal_lib, SharedLibrary *(string cm_path, string lib_name, Json::Value &binaries_info));
};
  

我是GTEST / GMOCK测试框架的新手。我正在写一个应用程序   它为我加载了共享对象(运行时为xxx.so)   使用Json::Value   类对象。这是一个开源库。

     

我想通过模拟加载库函数来测试应用程序   load_mal_lib。我为此编写了Mock类MockLoadLibTask   如上所述。但我面临编译错误。

     

注意:如果我评论模拟函数load_mal_lib   (MOCK_METHOD3(load_mal_lib,SharedLibrary *(string cm_path,string   lib_name,Json :: Value& binaries_info));)我的测试用例被编译   成功。

以下是编译错误日志。 (请帮我解决这个错误。我正在努力解决这个问题。)

In file included from /usr/local/include/gtest/internal/gtest-internal.h:39:0,
                 from /usr/local/include/gtest/gtest.h:58,
                 from tests/test_mal_router_run.cpp:3:
/usr/local/include/gtest/internal/gtest-port.h: In instantiation of ‘struct testing::internal::IteratorTraits<Json::ValueIterator>’:
/usr/local/include/gtest/internal/gtest-internal.h:909:3:   required from ‘struct testing::internal::IsRecursiveContainerImpl<Json::Value, true>’
/usr/local/include/gtest/internal/gtest-internal.h:920:8:   required from ‘struct testing::internal::IsRecursiveContainer<Json::Value>’
/usr/local/include/gtest/gtest-printers.h:513:69:   required from ‘void testing::internal::PrintTo(const T&, std::ostream*) [with T = Json::Value; std::ostream = std::basic_ostream<char>]’
/usr/local/include/gtest/gtest-printers.h:760:12:   required from ‘static void testing::internal::UniversalPrinter<T>::Print(const T&, std::ostream*) [with T = Json::Value; std::ostream = std::basic_ostream<char>]’
/usr/local/include/gtest/gtest-printers.h:929:30:   [ skipping 2 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/local/include/gmock/gmock-matchers.h:812:71:   required from ‘void testing::internal::ExplainMatchFailureTupleTo(const MatcherTuple&, const ValueTuple&, std::ostream*) [with MatcherTuple = std::tuple<testing::Matcher<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, testing::Matcher<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, testing::Matcher<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*> > >*>, testing::Matcher<Json::Value&> >; ValueTuple = std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*> > >*, Json::Value&>; std::ostream = std::basic_ostream<char>]’
/usr/local/include/gmock/gmock-spec-builders.h:1118:35:   required from ‘void testing::internal::TypedExpectation<F>::ExplainMatchResultTo(const ArgumentTuple&, std::ostream*) const [with F = bool(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>, std::map<std::__cxx11::basic_string<char>, void*>*, Json::Value&); testing::internal::TypedExpectation<F>::ArgumentTuple = std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*> > >*, Json::Value&>; std::ostream = std::basic_ostream<char>]’
/usr/local/include/gmock/gmock-spec-builders.h:1764:7:   required from ‘void testing::internal::FunctionMockerBase<F>::PrintTriedExpectationsLocked(const ArgumentTuple&, std::ostream*) const [with F = bool(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>, std::map<std::__cxx11::basic_string<char>, void*>*, Json::Value&); testing::internal::FunctionMockerBase<F>::ArgumentTuple = std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*> > >*, Json::Value&>; std::ostream = std::basic_ostream<char>]’
/usr/local/include/gmock/gmock-spec-builders.h:1740:33:   required from ‘void testing::internal::FunctionMockerBase<F>::FormatUnexpectedCallMessageLocked(const ArgumentTuple&, std::ostream*, std::ostream*) const [with F = bool(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>, std::map<std::__cxx11::basic_string<char>, void*>*, Json::Value&); testing::internal::FunctionMockerBase<F>::ArgumentTuple = std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*> > >*, Json::Value&>; std::ostream = std::basic_ostream<char>]’
/usr/local/include/gmock/gmock-spec-builders.h:1690:7:   required from ‘const testing::internal::ExpectationBase* testing::internal::FunctionMockerBase<F>::UntypedFindMatchingExpectation(const void*, const void**, bool*, std::ostream*, std::ostream*) [with F = bool(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>, std::map<std::__cxx11::basic_string<char>, void*>*, Json::Value&); std::ostream = std::basic_ostream<char>]’
tests/test_mal_router_run.cpp:124:1:   required from here
/usr/local/include/gtest/internal/gtest-port.h:2340:41: error: no type named ‘value_type’ in ‘class Json::ValueIterator’
   typedef typename Iterator::value_type value_type;

In file included from /usr/local/include/gtest/internal/gtest-param-util.h:47:0,                                                                                               [1/4825]
                 from /usr/local/include/gtest/gtest-param-test.h:190,
                 from /usr/local/include/gtest/gtest.h:62,
                 from tests/test_mal_router_run.cpp:3:
/usr/local/include/gtest/gtest-printers.h: In instantiation of ‘void testing::internal::PrintTo(const T&, std::ostream*) [with T = Json::Value; std::ostream = std::basic_ostream<char>
]’:
/usr/local/include/gtest/gtest-printers.h:760:12:   required from ‘static void testing::internal::UniversalPrinter<T>::Print(const T&, std::ostream*) [with T = Json::Value; std::ostre
am = std::basic_ostream<char>]’
/usr/local/include/gtest/gtest-printers.h:929:30:   required from ‘void testing::internal::UniversalPrint(const T&, std::ostream*) [with T = Json::Value; std::ostream = std::basic_ost
ream<char>]’
/usr/local/include/gmock/gmock-matchers.h:766:31:   required from ‘static void testing::internal::TuplePrefix<N>::ExplainMatchFailuresTo(const MatcherTuple&, const ValueTuple&, std::o
stream*) [with MatcherTuple = std::tuple<testing::Matcher<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, testing::Matcher<std::__cxx11::basic_strin
g<char, std::char_traits<char>, std::allocator<char> > >, testing::Matcher<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*, std::less<s
td::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocato
r<char> >, void*> > >*>, testing::Matcher<Json::Value&> >; ValueTuple = std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic
_string<char, std::char_traits<char>, std::allocator<char> >, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*, std::less<std::__cxx11::
basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, vo
id*> > >*, Json::Value&>; long unsigned int N = 4ul; std::ostream = std::basic_ostream<char>]’
/usr/local/include/gmock/gmock-matchers.h:812:71:   required from ‘void testing::internal::ExplainMatchFailureTupleTo(const MatcherTuple&, const ValueTuple&, std::ostream*) [with Matc
herTuple = std::tuple<testing::Matcher<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, testing::Matcher<std::__cxx11::basic_string<char, std::char_t
raits<char>, std::allocator<char> > >, testing::Matcher<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*, std::less<std::__cxx11::basic_
string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*> >
 >*>, testing::Matcher<Json::Value&> >; ValueTuple = std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::
char_traits<char>, std::allocator<char> >, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*, std::less<std::__cxx11::basic_string<char, 
std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*> > >*, Json::Va
lue&>; std::ostream = std::basic_ostream<char>]’
/usr/local/include/gmock/gmock-spec-builders.h:1118:35:   required from ‘void testing::internal::TypedExpectation<F>::ExplainMatchResultTo(const ArgumentTuple&, std::ostream*) const [
with F = bool(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>, std::map<std::__cxx11::basic_string<char>, void*>*, Json::Value&); testing::internal::TypedExpectatio
n<F>::ArgumentTuple = std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocat
or<char> >, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::al
locator<char> > >, std::allocator<std::pair<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*> > >*, Json::Value&>; std::ostream = std::basi
c_ostream<char>]’
/usr/local/include/gmock/gmock-spec-builders.h:1764:7:   required from ‘void testing::internal::FunctionMockerBase<F>::PrintTriedExpectationsLocked(const ArgumentTuple&, std::ostream*
) const [with F = bool(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>, std::map<std::__cxx11::basic_string<char>, void*>*, Json::Value&); testing::internal::Functi
onMockerBase<F>::ArgumentTuple = std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, s
td::allocator<char> >, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*, std::less<std::__cxx11::basic_string<char, std::char_traits<cha
r>, std::allocator<char> > >, std::allocator<std::pair<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*> > >*, Json::Value&>; std::ostream 
= std::basic_ostream<char>]’
/usr/local/include/gmock/gmock-spec-builders.h:1740:33:   required from ‘void testing::internal::FunctionMockerBase<F>::FormatUnexpectedCallMessageLocked(const ArgumentTuple&, std::os
tream*, std::ostream*) const [with F = bool(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>, std::map<std::__cxx11::basic_string<char>, void*>*, Json::Value&); test
ing::internal::FunctionMockerBase<F>::ArgumentTuple = std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std:
:char_traits<char>, std::allocator<char> >, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*, std::less<std::__cxx11::basic_string<char,
 std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*> > >*, Json::V
alue&>; std::ostream = std::basic_ostream<char>]’
/usr/local/include/gmock/gmock-spec-builders.h:1690:7:   required from ‘const testing::internal::ExpectationBase* testing::internal::FunctionMockerBase<F>::UntypedFindMatchingExpectat
ion(const void*, const void**, bool*, std::ostream*, std::ostream*) [with F = bool(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>, std::map<std::__cxx11::basic_str
ing<char>, void*>*, Json::Value&); std::ostream = std::basic_ostream<char>]’
tests/test_mal_router_run.cpp:124:1:   required from here
/usr/local/include/gtest/gtest-printers.h:513:69: error: ‘value’ is not a member of ‘testing::internal::IsRecursiveContainer<Json::Value>’
           (sizeof(IsContainerTest<T>(0)) == sizeof(IsContainer)) && !IsRecursiveContainer<T>::value
                                                                     ^
../../../Makefile.common:104: recipe for target 'tests/test_mal_router_run.o' failed
make: *** [tests/test_mal_router_run.o] Error 1

0 个答案:

没有答案