2016-08-17 2 views
2

S'il vous plaît considérer l'exemple de code ci-dessousgmock gtest comment configurer la maquette

NodeInterface * pPreNode = NULL; 
NodeInterface * pChild = NULL; 

for (uint16_t Index = 0; Index < Children.size(); ++Index) 
{ 
    pChild = Children[Index]; 
    if (pPreNode == NULL) 
    { 
     pChild->SetPrevious(pChild); 
     pChild->SetNext(pChild); 
    } 
    else 
    { 
     pChild->SetNext(pPreNode->GetNext()); 
     pChild->SetPrevious(pPreNode); 
     pPreNode->GetNext()->SetPrevious(pChild); 
     pPreNode->SetNext(pChild); 
    } 
    pPreNode = pChild; 
} 

Pour tester ces lignes comment configurer la maquette exactement? Children est un vector de nœuds et nous passons des objets mockés.

EXPECT_CALL(Obj, GetNode()).WillOnce(Invoke(this, &GetANewNode)); 

et GetANewNode fournira de nouvelles MockedNode

MockedNode * GetANewNode() 
{ 
    MockedNode * pMockedNode = new MockedNode(); 
    return pMockedNode; 
} 

Comment fournir des noeuds exacts pour chaque Next(), Previous() appels?

EXPECT_CALL(*pMockedNode, SetNext(_)); 
EXPECT_CALL(*pMockedNode, SetPrevious(_)); 
EXPECT_CALL(*pMockedNode, GetNext()); 
EXPECT_CALL(*pMockedNode, GetPrevious()); 

Répondre

1

Une solution simple consiste à avoir prédéfini tous les nœuds simulés avant le test. Et utilisez Sequence/InSequence pour être sûr que tout se passe dans le bon ordre.

class ObjTest : public ::testing::Test 
{ 
protected: 
    const std::size_t N = ...; // I do not know how many do you need 
    std::vector<MockedNode> mockedNode; 
    std::vector<Node*> children; 
    Sequence s; 
    .... Obj; // I am not sure what is Obj in your question 
    ObjTest() : mockedNode(N) 
    {} 
    void SetUp() override 
    { 
     // initial setup 
     EXPECT_CALL(Obj, GetNode()).WillOnce(Return(&mockedNode.front())).InSequence(s); 
    } 
}; 

Avoir cette classe de test avec la configuration initiale - vous pouvez créer des cas de test de tester différents scénarios qui se produisent après la séquence initiale:

TEST_F(ObjTest, shouldLinkOneNodeToItself) 
{ 
    std::vector<Node*> children { &mockedNode[0] }; 
    EXPECT_CALL(mockedNode[0], SetNext(&mockedNode[0])).InSequence(s); 
    EXPECT_CALL(mockedNode[0], SetPrevious(&mockedNode[0])).InSequence(s); 
    Obj.testedFunction(children); // you have not provided your tested function name... 
} 

Et très similaire test pour deux enfants:

TEST_F(ObjTest, shouldLinkTwoNodesToEachOther) 
{ 
    std::vector<Node*> children { &mockedNode[0], &&mockedNode[1] }; 
    // first interation 
    EXPECT_CALL(mockedNode[0], SetNext(&mockedNode[0])).InSequence(s); 
    EXPECT_CALL(mockedNode[0], SetPrevious(&mockedNode[0])).InSequence(s); 
    // second iteration 
    EXPECT_CALL(mockedNode[0], GetNext()).WillOnce(Return(&mockedNode[0])).InSequence(s); 
    EXPECT_CALL(mockedNode[1], SetNext(&mockedNode[0])).InSequence(s); 
    EXPECT_CALL(mockedNode[1], SetPrevious(&mockedNode[0])).InSequence(s); 
    // etc... 

    Obj.testedFunction(children); 
}