Question

I am new to the GTEST, just understanding the how the Mock works,

i tried to write the simple program Foo.h and FooDisplay.h ( which needs the Foo in the constructor), also MockFoo.cpp ( which is the main GTEST program to test the Gmock)..

when i mock and do the Expect call for the Foo, it throws below in the execution..

Logs

[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from Foo
[ RUN      ] Foo.case1
package/web/webscr/MockFoo.cpp:19: Failure
Actual function call count doesn't match EXPECT_CALL(mock, GetSize(_))...
         Expected: to be called once
           Actual: never called - unsatisfied and active
[  FAILED  ] Foo.case1 (0 ms)
[----------] 1 test from Foo (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  ] Foo.case1

Foo.h

#include <gtest/gtest.h>
#include <infra/utility/core/lang/PPException.h>
#include <infra/utility/core/lang/String.h>
#include <iostream>

class Foo {

public:
    virtual ~Foo()
    { }

    virtual int GetSize(int size)
    {
        return size;
    }

};

FooDisplay.h

#include <gtest/gtest.h>
#include <infra/utility/core/lang/PPException.h>
#include <infra/utility/core/lang/String.h>
#include <iostream>
#include "Foo.h"

class FooDisplay {

public:
    FooDisplay ( Foo _foo) : foo(_foo)
    { }

    virtual ~FooDisplay()
    { }

    virtual String Describe(int size)
    {
        foo.GetSize(size);
        String str = "Done";
        return str;
    }
private:
    Foo foo;
};

MooFoo.cpp

#include "gmock/gmock.h"
#include <infra/utility/core/lang/String.h>
#include <iostream>
#include "FooDisplay.h"

using ::testing::Return;
using ::testing::_;

class MockFoo : public Foo {
public:
  MOCK_CONST_METHOD1(GetSize, int(int size));
  //MOCK_CONST_METHOD1(Describe, String(int));
};

TEST(Foo,case1)
{
        MockFoo mockfoo;
        FooDisplay Display(mockfoo);

        EXPECT_CALL(mockfoo,GetSize(_)).WillOnce(Return(6));
        Display.Describe(5);
//      ON_CALL(Display,Describe(5)).WillByDefault( Return ("Hi"));
}

int main(int argc, char * argv[])
{
     ::testing::InitGoogleTest(&argc,argv);
     return RUN_ALL_TESTS();
     //return 1;
 }
Was it helpful?

Solution

There are two bugs in your code, meaning you are lacking c++ basics.

  1. The first bug is in your mock class. The GetSize() method in base class Foo is declared as :
    virtual int GetSize(int size)
    but in your mock it is :
    MOCK_CONST_METHOD1(GetSize, int(int size));
    meaning
    virtual int GetSize(int size) const
    These two methods have different signatures, and because of that, MockFoo is not overriding the Foo's method.

  2. The second bug is when you pass MockFoo's object into DisplayFoo's constructor. What I think you wanted to do is to use MockFoo object, but you accidentally copied it's base object. Changing FooDisplay to this should solve this issue :

    class FooDisplay {
    public:
        FooDisplay (Foo & _foo) : foo (_foo) { }
        virtual ~FooDisplay () { }
    
        virtual std::string Describe (int size) {
            foo.GetSize (size);
            std::string str = "Done";
            return str;
        }
    
        private:
            Foo & foo;
    };
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top