我是 GTEST 的新手,只是了解 Mock 的工作原理,
我尝试编写简单的程序 Foo.h 和 FooDisplay.h (它需要在构造函数中的 Foo ),还有 MockFoo.cpp (这是测试 Gmock 的主要 GTEST 程序)..
当我模拟并为 Foo 执行 Expect 调用时,它会在执行中抛出以下内容..
日志
[==========] 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;
}