Vous avez besoin de vos commentaires concernant ce qui suit: Je suis confronté à un problème concernant la façon d'écrire un faux pour une classe de base (StackBT) dont une instance est créée dans le constructeur de la classe dérivée (ApplicationBT) que je veux tester. Mon intention est d'écrire un simulacre pour la classe StackBT (Mock_StackBT), puis de le lier au test unitaire afin que l'instance du mock soit créée en faisant "new StackBT()" dans le constructeur d'ApplicationBT. Donc, en utilisant cela, je peux mocker les attentes sur la classe StackBT tout en testant la classe ApplicationBT.Se moquer et s'attendre à une instance de classe créée en utilisant un nouvel opérateur dans Google Mock
out/linux_host/obj/TestApplicationBT.o: In function `TestApplicationBT::SetUp()':
tst/_src/TestApplicationBT.cpp:33: undefined reference to `mockPtr_StackBT'
out/linux_host/lib/libServer.a(ApplicationBT.o): In function `ApplicationBT::init()':
/_src/ApplicationBT.cpp:36: undefined reference to `StackBT::registerCallbacks()'
/_src/ApplicationBT.cpp:43: undefined reference to `StackBT::sendBTMacAddress(std::string)'
collect2: error: ld returned 1 exit status
make: *** [out/linux_host/bin/Test] Error 1
Je reçois l'erreur de compilation ci-dessus lors de la compilation l'extrait de code ci-dessous:
StackBT.h:
class StackBT
{
StackBT(){}
void registerCallbacks();
void sendBTMacAddress(std::string str);
}
Mock_StackBT.h:
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include <string>
using ::testing::NiceMock;
class Mock_StackBT;
extern NiceMock <Mock_StackBT>* mockPtr_StackBT;
class Mock_StackBT: public StackBT
{
Mock_StackBT(){}
MOCK_METHOD0(registerCallbacks, void());
MOCK_METHOD1(sendBTMacAddress, void(std::string str));
}
Mock_StackBT.cpp:
#include "Mock_StackBT.h"
NiceMock <Mock_StackBT>* mockPtr_StackBT;
void registerCallbacks()
{
mockPtr_StackBT->registerCallbacks();
}
void sendBTMacAddress(std::string str)
{
mockPtr_StackBT->sendBTMacAddress(std::string str);
}
ApplicationBT.h:
class ApplicationBT
{
public:
ApplicationBT() : mpoStackBT(new StackBT())
void init()
{
mpoStackBT->registerCallbacks();
mpoStackBT->sendBTMacAddress("AB:CD:EF:GH:IJ:KL");
}
friend class TestApplicationBT;
scoped_ptr<StackBT> mpoStackBT;
}
TestApplicationBT.h
class TestApplicationBT : public ::testing::Test
{
protected:
virtual void SetUp()
{
mockPtr_StackBT = &stackBTMock;
ptrApplicationBT = new ApplicationBT();
}
void TearDown()
{
delete ptrApplicationBT;
}
public:
TestApplicationBT()
{
}
~TestApplicationBT()
{
ptrApplicationBT = NULL;
}
scoped_ptr<ApplicationBT> ptrApplicationBT;
StackBT* ptrStackBT;
NiceMock<Mock_StackBT> stackBTMock;
};
TEST_F(TestApplicationBT, Init)
{
EXPECT_CALL(stackBTMock, registerCallbacks()).Times(1);
EXPECT_CALL(stackBTMock, sendBTMacAddress(_)).Times(1);
ptrApplicationBT->init();
}
Merci beaucoup :) – user1032187
@ user1032187 Vous êtes les bienvenus! Veuillez marquer la réponse comme acceptée si cela vous a été utile. –