对于模拟方法,GoogleTest Expect调用失败

时间:2013-03-20 06:11:47

标签: c++ unit-testing mocking googletest googlemock

我是GTEST的新手,只是了解Mock的工作方式,

我试着编写简单的程序Foo.h和FooDisplay.h(需要构造函数中的Foo),还有MockFoo.cpp(这是测试Gmock的主要GTEST程序)..

当我模拟并执行Expect对Foo的调用时,它会在执行中抛出以下内容。

日志

[==========] 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;
 }

1 个答案:

答案 0 :(得分:4)

您的代码中有两个错误,这意味着您缺少c ++基础知识。

  1. 第一个错误发生在你的模拟类中。基类GetSize()中的Foo方法声明为:
    virtual int GetSize(int size)
    但在你的模拟中它是:
    MOCK_CONST_METHOD1(GetSize, int(int size));
    这意味着
    virtual int GetSize(int size) const
    这两种方法有不同的签名,因此,MockFoo不会覆盖Foo的方法。

  2. 第二个错误是当你将MockFoo的对象传递给DisplayFoo的构造函数时。我认为你想要做的是使用MockFoo对象,但你不小心复制了它的基础对象。将FooDisplay更改为此应解决此问题:

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