How to generate a mocked object that is created within the class being tested?

1,559 views
Skip to first unread message

Jack Adrian Zappa

unread,
Sep 17, 2015, 8:23:43 PM9/17/15
to Google C++ Mocking Framework

I have a testing class that instantiates a class that I wish to be mocked. I was able to replace it with another class without problem. Is this possible within the gmock framework? From what I read, it seems that the mocked object has to be created and passed to the object being tested and that is not what I want.


This is what I have:

#include "gmock/gmock.h"

class class_to_be_mocked
{
public:
 class_to_be_mocked
();
 
~class_to_be_mocked();

 MOCK_CONST_METHOD0
(result, bool());
 MOCK_CONST_METHOD0
(result2, bool());
};



I was thinking about putting this into the constructor:

class_to_be_mocked::class_to_be_mocked()
{
 EXPECT_CALL
(*this, result()).Times(1);
 EXPECT_CALL
(*this, result()).Times(1);
}


But when I link I get:

1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl testing::internal::FormatFileLocation(char const *,int)" (?FormatFileLocation@internal@testing@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PBDH@Z) referenced in function "private: void __thiscall testing::internal::FunctionMockerBase<bool __cdecl(void)>::DescribeDefaultActionTo(class std::tuple<> const &,class std::basic_ostream<char,struct std::char_traits<char> > *)const " (?DescribeDefaultActionTo@?$FunctionMockerBase@$$A6A_NXZ@internal@testing@@ABEXABV?$tuple@$$$V@std@@PAV?$basic_ostream@DU?$char_traits@D@std@@@5@@Z)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "class testing::internal::FailureReporterInterface * __cdecl testing::internal::GetFailureReporter(void)" (?GetFailureReporter@internal@testing@@YAPAVFailureReporterInterface@12@XZ) referenced in function "void __cdecl testing::internal::Assert(bool,char const *,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?Assert@internal@testing@@YAX_NPBDHABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "void __cdecl testing::internal::Log(enum testing::internal::LogSeverity,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int)" (?Log@internal@testing@@YAXW4LogSeverity@12@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z) referenced in function "private: class testing::Action<bool __cdecl(void)> const & __thiscall testing::internal::TypedExpectation<bool __cdecl(void)>::GetCurrentAction(class testing::internal::FunctionMockerBase<bool __cdecl(void)> const *,class std::tuple<> const &)const " (?GetCurrentAction@?$TypedExpectation@$$A6A_NXZ@internal@testing@@ABEABV?$Action@$$A6A_NXZ@3@PBV?$FunctionMockerBase@$$A6A_NXZ@23@ABV?$tuple@$$$V@std@@@Z)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "class testing::Cardinality __cdecl testing::Exactly(int)" (?Exactly@testing@@YA?AVCardinality@1@H@Z) referenced in function "public: class testing::internal::TypedExpectation<bool __cdecl(void)> & __thiscall testing::internal::TypedExpectation<bool __cdecl(void)>::Times(int)" (?Times@?$TypedExpectation@$$A6A_NXZ@internal@testing@@QAEAAV123@H@Z)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "public: __thiscall testing::internal::UntypedFunctionMockerBase::UntypedFunctionMockerBase(void)" (??0UntypedFunctionMockerBase@internal@testing@@QAE@XZ) referenced in function "public: __thiscall testing::internal::FunctionMockerBase<bool __cdecl(void)>::FunctionMockerBase<bool __cdecl(void)>(void)" (??0?$FunctionMockerBase@$$A6A_NXZ@internal@testing@@QAE@XZ)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall testing::internal::UntypedFunctionMockerBase::~UntypedFunctionMockerBase(void)" (??1UntypedFunctionMockerBase@internal@testing@@UAE@XZ) referenced in function __unwindfunclet$??0?$FunctionMockerBase@$$A6A_NXZ@internal@testing@@QAE@XZ$0
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "public: bool __thiscall testing::internal::UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked(void)" (?VerifyAndClearExpectationsLocked@UntypedFunctionMockerBase@internal@testing@@QAE_NXZ) referenced in function "public: virtual __thiscall testing::internal::FunctionMockerBase<bool __cdecl(void)>::~FunctionMockerBase<bool __cdecl(void)>(void)" (??1?$FunctionMockerBase@$$A6A_NXZ@internal@testing@@UAE@XZ)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "public: void __thiscall testing::internal::UntypedFunctionMockerBase::RegisterOwner(void const *)" (?RegisterOwner@UntypedFunctionMockerBase@internal@testing@@QAEXPBX@Z) referenced in function "public: class testing::internal::MockSpec<bool __cdecl(void)> & __thiscall class_to_be_mocked::gmock_result(void)const " (?gmock_result@class_to_be_mocked@@QBEAAV?$MockSpec@$$A6A_NXZ@internal@testing@@XZ)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "public: void const * __thiscall testing::internal::UntypedFunctionMockerBase::MockObject(void)const " (?MockObject@UntypedFunctionMockerBase@internal@testing@@QBEPBXXZ) referenced in function "protected: class testing::internal::TypedExpectation<bool __cdecl(void)> & __thiscall testing::internal::FunctionMockerBase<bool __cdecl(void)>::AddNewExpectation(char const *,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::tuple<> const &)" (?AddNewExpectation@?$FunctionMockerBase@$$A6A_NXZ@internal@testing@@IAEAAV?$TypedExpectation@$$A6A_NXZ@23@PBDHABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV?$tuple@$$$V@6@@Z)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "public: char const * __thiscall testing::internal::UntypedFunctionMockerBase::Name(void)const " (?Name@UntypedFunctionMockerBase@internal@testing@@QBEPBDXZ) referenced in function "private: virtual void __thiscall testing::internal::FunctionMockerBase<bool __cdecl(void)>::UntypedDescribeUninterestingCall(void const *,class std::basic_ostream<char,struct std::char_traits<char> > *)const " (?UntypedDescribeUninterestingCall@?$FunctionMockerBase@$$A6A_NXZ@internal@testing@@EBEXPBXPAV?$basic_ostream@DU?$char_traits@D@std@@@std@@@Z)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "protected: class testing::Expectation __thiscall testing::internal::UntypedFunctionMockerBase::GetHandleOf(class testing::internal::ExpectationBase *)" (?GetHandleOf@UntypedFunctionMockerBase@internal@testing@@IAE?AVExpectation@3@PAVExpectationBase@23@@Z) referenced in function "private: virtual class testing::Expectation __thiscall testing::internal::TypedExpectation<bool __cdecl(void)>::GetHandle(void)" (?GetHandle@?$TypedExpectation@$$A6A_NXZ@internal@testing@@EAE?AVExpectation@3@XZ)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "private: static void __cdecl testing::Mock::RegisterUseByOnCallOrExpectCall(void const *,char const *,int)" (?RegisterUseByOnCallOrExpectCall@Mock@testing@@CAXPBXPBDH@Z) referenced in function "protected: class testing::internal::TypedExpectation<bool __cdecl(void)> & __thiscall testing::internal::FunctionMockerBase<bool __cdecl(void)>::AddNewExpectation(char const *,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::tuple<> const &)" (?AddNewExpectation@?$FunctionMockerBase@$$A6A_NXZ@internal@testing@@IAEAAV?$TypedExpectation@$$A6A_NXZ@23@PBDHABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV?$tuple@$$$V@6@@Z)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "private: static void __cdecl testing::Mock::UnregisterLocked(class testing::internal::UntypedFunctionMockerBase *)" (?UnregisterLocked@Mock@testing@@CAXPAVUntypedFunctionMockerBase@internal@2@@Z) referenced in function "public: virtual __thiscall testing::internal::FunctionMockerBase<bool __cdecl(void)>::~FunctionMockerBase<bool __cdecl(void)>(void)" (??1?$FunctionMockerBase@$$A6A_NXZ@internal@testing@@UAE@XZ)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "public: __thiscall testing::Expectation::~Expectation(void)" (??1Expectation@testing@@QAE@XZ) referenced in function "public: void * __thiscall testing::Expectation::`scalar deleting destructor'(unsigned int)" (??_GExpectation@testing@@QAEPAXI@Z)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "private: __thiscall testing::Expectation::Expectation(class testing::internal::linked_ptr<class testing::internal::ExpectationBase> const &)" (??0Expectation@testing@@AAE@ABV?$linked_ptr@VExpectationBase@internal@testing@@@internal@1@@Z) referenced in function "protected: class testing::internal::TypedExpectation<bool __cdecl(void)> & __thiscall testing::internal::FunctionMockerBase<bool __cdecl(void)>::AddNewExpectation(char const *,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::tuple<> const &)" (?AddNewExpectation@?$FunctionMockerBase@$$A6A_NXZ@internal@testing@@IAEAAV?$TypedExpectation@$$A6A_NXZ@23@PBDHABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV?$tuple@$$$V@6@@Z)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "public: void __thiscall testing::Sequence::AddExpectation(class testing::Expectation const &)const " (?AddExpectation@Sequence@testing@@QBEXABVExpectation@2@@Z) referenced in function "protected: class testing::internal::TypedExpectation<bool __cdecl(void)> & __thiscall testing::internal::FunctionMockerBase<bool __cdecl(void)>::AddNewExpectation(char const *,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::tuple<> const &)" (?AddNewExpectation@?$FunctionMockerBase@$$A6A_NXZ@internal@testing@@IAEAAV?$TypedExpectation@$$A6A_NXZ@23@PBDHABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV?$tuple@$$$V@6@@Z)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "public: __thiscall testing::internal::ExpectationBase::ExpectationBase(char const *,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0ExpectationBase@internal@testing@@QAE@PBDHABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __thiscall testing::internal::TypedExpectation<bool __cdecl(void)>::TypedExpectation<bool __cdecl(void)>(class testing::internal::FunctionMockerBase<bool __cdecl(void)> *,char const *,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::tuple<> const &)" (??0?$TypedExpectation@$$A6A_NXZ@internal@testing@@QAE@PAV?$FunctionMockerBase@$$A6A_NXZ@12@PBDHABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV?$tuple@$$$V@5@@Z)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall testing::internal::ExpectationBase::~ExpectationBase(void)" (??1ExpectationBase@internal@testing@@UAE@XZ) referenced in function __unwindfunclet$??0?$TypedExpectation@$$A6A_NXZ@internal@testing@@QAE@PAV?$FunctionMockerBase@$$A6A_NXZ@12@PBDHABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV?$tuple@$$$V@5@@Z$0
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "public: void __thiscall testing::internal::ExpectationBase::DescribeCallCountTo(class std::basic_ostream<char,struct std::char_traits<char> > *)const " (?DescribeCallCountTo@ExpectationBase@internal@testing@@QBEXPAV?$basic_ostream@DU?$char_traits@D@std@@@std@@@Z) referenced in function "private: class testing::Action<bool __cdecl(void)> const * __thiscall testing::internal::TypedExpectation<bool __cdecl(void)>::GetActionForArguments(class testing::internal::FunctionMockerBase<bool __cdecl(void)> const *,class std::tuple<> const &,class std::basic_ostream<char,struct std::char_traits<char> > *,class std::basic_ostream<char,struct std::char_traits<char> > *)" (?GetActionForArguments@?$TypedExpectation@$$A6A_NXZ@internal@testing@@AAEPBV?$Action@$$A6A_NXZ@3@PBV?$FunctionMockerBase@$$A6A_NXZ@23@ABV?$tuple@$$$V@std@@PAV?$basic_ostream@DU?$char_traits@D@std@@@7@2@Z)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "protected: void __thiscall testing::internal::ExpectationBase::RetireAllPreRequisites(void)" (?RetireAllPreRequisites@ExpectationBase@internal@testing@@IAEXXZ) referenced in function "private: class testing::Action<bool __cdecl(void)> const * __thiscall testing::internal::TypedExpectation<bool __cdecl(void)>::GetActionForArguments(class testing::internal::FunctionMockerBase<bool __cdecl(void)> const *,class std::tuple<> const &,class std::basic_ostream<char,struct std::char_traits<char> > *,class std::basic_ostream<char,struct std::char_traits<char> > *)" (?GetActionForArguments@?$TypedExpectation@$$A6A_NXZ@internal@testing@@AAEPBV?$Action@$$A6A_NXZ@3@PBV?$FunctionMockerBase@$$A6A_NXZ@23@ABV?$tuple@$$$V@std@@PAV?$basic_ostream@DU?$char_traits@D@std@@@7@2@Z)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "protected: bool __thiscall testing::internal::ExpectationBase::AllPrerequisitesAreSatisfied(void)const " (?AllPrerequisitesAreSatisfied@ExpectationBase@internal@testing@@IBE_NXZ) referenced in function "private: void __thiscall testing::internal::TypedExpectation<bool __cdecl(void)>::ExplainMatchResultTo(class std::tuple<> const &,class std::basic_ostream<char,struct std::char_traits<char> > *)const " (?ExplainMatchResultTo@?$TypedExpectation@$$A6A_NXZ@internal@testing@@ABEXABV?$tuple@$$$V@std@@PAV?$basic_ostream@DU?$char_traits@D@std@@@5@@Z)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "protected: void __thiscall testing::internal::ExpectationBase::FindUnsatisfiedPrerequisites(class testing::ExpectationSet *)const " (?FindUnsatisfiedPrerequisites@ExpectationBase@internal@testing@@IBEXPAVExpectationSet@3@@Z) referenced in function "private: void __thiscall testing::internal::TypedExpectation<bool __cdecl(void)>::ExplainMatchResultTo(class std::tuple<> const &,class std::basic_ostream<char,struct std::char_traits<char> > *)const " (?ExplainMatchResultTo@?$TypedExpectation@$$A6A_NXZ@internal@testing@@ABEXABV?$tuple@$$$V@std@@PAV?$basic_ostream@DU?$char_traits@D@std@@@5@@Z)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "protected: void __thiscall testing::internal::ExpectationBase::CheckActionCountIfNotDone(void)const " (?CheckActionCountIfNotDone@ExpectationBase@internal@testing@@IBEXXZ) referenced in function "public: virtual __thiscall testing::internal::TypedExpectation<bool __cdecl(void)>::~TypedExpectation<bool __cdecl(void)>(void)" (??1?$TypedExpectation@$$A6A_NXZ@internal@testing@@UAE@XZ)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "protected: void __thiscall testing::internal::ExpectationBase::UntypedTimes(class testing::Cardinality const &)" (?UntypedTimes@ExpectationBase@internal@testing@@IAEXABVCardinality@3@@Z) referenced in function "public: class testing::internal::TypedExpectation<bool __cdecl(void)> & __thiscall testing::internal::TypedExpectation<bool __cdecl(void)>::Times(class testing::Cardinality const &)" (?Times@?$TypedExpectation@$$A6A_NXZ@internal@testing@@QAEAAV123@ABVCardinality@3@@Z)
1>m_class_to_be_mocked.obj : error LNK2019: unresolved external symbol "void __cdecl testing::internal::LogWithLocation(enum testing::internal::LogSeverity,char const *,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?LogWithLocation@internal@testing@@YAXW4LogSeverity@12@PBDHABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: class testing::internal::TypedExpectation<bool __cdecl(void)> & __thiscall testing::internal::MockSpec<bool __cdecl(void)>::InternalExpectedAt(char const *,int,char const *,char const *)" (?InternalExpectedAt@?$MockSpec@$$A6A_NXZ@internal@testing@@QAEAAV?$TypedExpectation@$$A6A_NXZ@23@PBDH00@Z)
1>m_class_to_be_mocked.obj : error LNK2001: unresolved external symbol "class testing::internal::Mutex testing::internal::g_linked_ptr_mutex" (?g_linked_ptr_mutex@internal@testing@@3VMutex@12@A)
1>m_class_to_be_mocked.obj : error LNK2001: unresolved external symbol "class testing::internal::Mutex testing::internal::g_gmock_mutex" (?g_gmock_mutex@internal@testing@@3VMutex@12@A)
1>m_class_to_be_mocked.obj : error LNK2001: unresolved external symbol "class testing::internal::ThreadLocal<class testing::Sequence *> testing::internal::g_gmock_implicit_sequence" (?g_gmock_implicit_sequence@internal@testing@@3V?$ThreadLocal@PAVSequence@testing@@@12@A)
1>unittest1.obj : error LNK2019: unresolved external symbol "public: void __thiscall testing::internal::UntypedFunctionMockerBase::SetOwnerAndName(void const *,char const *)" (?SetOwnerAndName@UntypedFunctionMockerBase@internal@testing@@QAEXPBXPBD@Z) referenced in function "public: bool __thiscall class_to_be_mocked::result2(void)const " (?result2@class_to_be_mocked@@QBE_NXZ)
1>unittest1.obj : error LNK2019: unresolved external symbol "public: class testing::internal::UntypedActionResultHolderBase const * __thiscall testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const *)" (?UntypedInvokeWith@UntypedFunctionMockerBase@internal@testing@@QAEPBVUntypedActionResultHolderBase@23@PBX@Z) referenced in function "protected: bool __thiscall testing::internal::FunctionMockerBase<bool __cdecl(void)>::InvokeWith(class std::tuple<> const &)" (?InvokeWith@?$FunctionMockerBase@$$A6A_NXZ@internal@testing@@IAE_NABV?$tuple@$$$V@std@@@Z)
1>unittest1.obj : error LNK2019: unresolved external symbol "void __cdecl testing::InitGoogleMock(int *,char * *)" (?InitGoogleMock@testing@@YAXPAHPAPAD@Z) referenced in function "void __stdcall ModuleInitialize(void)" (?ModuleInitialize@@YGXXZ)
1>C:\Users\Jack\Documents\Visual Studio 2013\Projects\UnitTest1\Debug\UnitTest1.dll : fatal error LNK1120: 31 unresolved externals


I'm not sure what I'm doing wrong.  Help would be appreciated.

Thanks,


JAZ

Billy Donahue

unread,
Sep 17, 2015, 8:26:04 PM9/17/15
to Jack Adrian Zappa, Google C++ Mocking Framework
It looks like you're not linking the googlemock library into your test executable.

What does your link command look like? Check it carefully.

--

---
You received this message because you are subscribed to the Google Groups "Google C++ Mocking Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googlemock+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/googlemock/0f52a9d7-e75b-42ec-8cd4-fa034d717c08%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages