3
我是新來的GTEST,只是瞭解如何素作品,GoogleTest期待致電失敗的模擬方法
我試着寫了簡單的程序了foo.h和FooDisplay.h(這需要在富構造),也MockFoo.cpp(這是主要GTEST程序來測試Gmock)..
當我嘲笑並執行期望調用爲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;
}
冷卻這是工作的感謝... – Gopal 2013-03-20 08:21:26