0
class A
{
    public:
        int ret1();
};
int ret()
{
    printf("Returning 5\n");
    return 5;
}

int A::ret1()
{
    int iRet = 10;
    printf("Inside ret1\n");
    iRet = ret();
    if (iRet == 5)
    {
        printf("Original ret is called\n");
    }
    else if (iRet == 100)
    {
        printf("This is mocked function call\n");
    }
    else
    {
        printf("Opps! This should not happen\n");
    }
    return iRet;
}

In the above ret1 function I would like to mock the return value of ret function's. Normally ret returns 5 but after mocking lets say ret will return 100 so ret1 will print "This is mocked function call".

Is there any tool to do the above situation. So as far I know cmocka can cover the above situation for C language but for C++ is there any tool available?

In the link Mocking free function doesn't look like answering my question.

Dipankar Saha
  • 302
  • 1
  • 5
  • 15
  • https://github.com/google/googletest include googlemock for creating mock objects. – Jepessen May 31 '17 at 06:39
  • I tried my best but couldn't find it. Can you please let me know which section or with an example mock code? – Dipankar Saha May 31 '17 at 07:12
  • @Jarod42 The above mentioned link doesn't look like it is alike. Can you please let me know how to mock the return value of `ret` while `ret1` is called? So the output would print "This is mocked function call" – Dipankar Saha May 31 '17 at 09:34
  • Something like [this](https://ideone.com/SHewh8) – Jarod42 May 31 '17 at 11:48
  • @Jarod42: I appreciate your effort. But this is not the change I am looking for. My base code which I mentioned in the question can not be changed. So with the help of mocking of `ret` function's return value I would like to have 100% line coverage of `ret1` function. – Dipankar Saha May 31 '17 at 13:10

0 Answers0