1

I am new to gtest and gmock please let me understand how to mock the called function. Which will also help me in code coverage.

#include <stdio.h>
#include "gtest/gtest.h"

int ret()
{
    return 5;
}

class A
{
    public:
        A();
        int a;
        int func();
};

A::A()
{
    printf("This is constructor\n");
}

int A::func()
{
    int iRet = ret(); /* When func() is called by gtest I would like to control the return value of ret() */
    int iRet1 = ret();
    /* Based on these two values some operations to be done */
    printf("This is func. %d, %d\n", iRet, iRet1);
    return iRet;
}

TEST (abc, xyz) {
    A a;
    EXPECT_EQ(5, a.func()); /* Here how to get different values of iRet and iRet1 for ret() function? */
}

int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

In case if it is not possible through gtest and/or gmock, please suggest me any other tool for the same.

Also I tried with the following thing which is not working as expected:

int ret()
{
    printf("Returning 5\n");
    return 5;
}

int 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;
}

class FooMock {
    public:
        MOCK_METHOD0(ret, int());
        MOCK_METHOD0(ret1, int());
};

TEST (abc, xyz) {
    FooMock mock;
    EXPECT_CALL(mock, ret()).Times(1).WillOnce(Return(100));
    mock.ret1();
}
int main(int argc, char **argv)
{
    ::testing::InitGoogleMock(&argc, argv);
    return RUN_ALL_TESTS();
}

While running giving me the following error:

$ ./a.out 
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from abc
[ RUN      ] abc.xyz
gtest.cpp:86: Failure
Actual function call count doesn't match EXPECT_CALL(mock, ret())...
         Expected: to be called once
           Actual: never called - unsatisfied and active
gtest.cpp:87: Failure
Actual function call count doesn't match EXPECT_CALL(mock, ret())...
         Expected: to be called once
           Actual: never called - unsatisfied and active
[  FAILED  ] abc.xyz (0 ms)
[----------] 1 test from abc (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] abc.xyz

 1 FAILED TEST

Please let me know if anything I am doing wrong...

Dipankar Saha
  • 302
  • 1
  • 5
  • 15
  • See the documentation for the concepts of Unit-Testing: https://stackoverflow.com/documentation/unit-testing/topics The idea is that you call your method with example data and compare the result to what you know is the correct result. At first, I would not concern myself with code-coverage, start writing good tests. – Gerriet May 30 '17 at 14:03

2 Answers2

0

So, the idea of unit-testing is that you run some of your code and that you compare the results to what you expect to be the result. Your code makes it more complicated than it has to be. E.g.

#include <stdio.h>
#include "gtest/gtest.h"

int incBy5(const int a) {
  return a+5;
}

TEST (abc, xyz) {
  int x=17;
  EXPECT_EQ(x+5, incBy5(x));
}

int main(int argc, char **argv)
{
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

See the documentation of unit testing concepts in general and google-test in particular. The mocking aspect of it all comes into place with more complex things to test where you may not have everything readily available (e.g. network resources, databases, ...) and you create mock-ups that can replace these resources.

Gerriet
  • 1,302
  • 14
  • 20
0

In your second snippet, you define two mock methods, ret and ret1. Later, you set up an expectation on ret but invoke ret1. At the end of the test, the expectation on ret remains unfulfilled, causing gmock to complain.

VladLosev
  • 7,296
  • 2
  • 35
  • 46
  • Thanks for your reply. What I am expecting is when `ret1` is called trying to control the return value of `ret` (`ret` gets called by `ret1`). Any idea? – Dipankar Saha May 31 '17 at 05:38
  • Note that gmock does not support directly mocking out free functions. It only mocks class methods, virtual ones. I suggest reading gmock's tutorial at https://github.com/google/googletest/blob/master/googlemock/docs/ForDummies.md#a-case-for-mock-turtles for better understanding how it works and how to use it. TL;DR: make `ret` and and `ret1` methods and mock out `ret`. – VladLosev Jun 02 '17 at 05:03