lundi 27 avril 2020

Actual function call count doesn't match EXPECT_CALL(mockImplClass, receive(_, _))

I am facing problem while running gtest for the following code sample. ignore header includes as its compilable and running fine.

Error:
GMOCK WARNING:
Uninteresting mock function call - returning default value.
    Function call: receive(0x7ffcee4fc990, 0x7ffcee4fc900)
          Returns: 0
NOTE: You can safely ignore the above warning unless this call should not happen.  Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call.  See https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#knowing-when-to-expect for details.
/data/home/sipadhy/unit_test_research/gTest/ImplClassTest.cpp:174: Failure
Actual function call count doesn't match EXPECT_CALL(mockImplClass, receive(_, _))...
         Expected: to be called at least once
           Actual: never called - unsatisfied and active

Sample Code:

// Main Class where function to be mocked

class ImplClass
{
public:
    virtual int receive(structX* x, structY* y){ // some logic }
};

// An intermidiate class which calls the main class

class IntermidiateClass
{
    std::shared_ptr<ImplClass> implClassPtr = nullptr;
public:
    setImplClassptr(std::shared_ptr<ImplClass> ptr)
    {
        implClassPtr  = ptr;
    }
    int getValue()
    {
        structX x;
        structY y;
        return(implClassPtr->receive(x, y));
    }
};

// Mock Class

class MockImplClass: public ImplClass
{
public:
    MOCK_METHOD2(receive, int(structX, structY));
}

// Test case

TEST(MyTest, TEST1)
{
    MockImplClass mockImplClass;
    IntermidiateClass intermidiateObj;
    intermidiateObj.setImplClassptr(std::make_shared<MockImplClass>());

    EXPECT_CALL(mockImplClass, receive(_, _))
    .Times(AtLeast(1))
    .WillRepeatedly(Return(1));

    int retVal = intermidiateObj.getValue();
}

Thanks, Siva

Aucun commentaire:

Enregistrer un commentaire