1 #include <cmock/cmock.h>
5 using namespace ::testing;
7 DECLARE_FUNCTION_MOCK2(AddFunctionMock, add, int(int, int));
8 DECLARE_FUNCTION_MOCK1(NegateFunctionMock, negate, int(int));
10 IMPLEMENT_FUNCTION_MOCK2(AddFunctionMock, add, int(int, int));
11 IMPLEMENT_FUNCTION_MOCK1(NegateFunctionMock, negate, int(int));
14 * Function add is mocked as long as AddFunctionMock instance exists.
15 * Once mock function is destroyed, a call directs to a real function.
17 TEST(FunctionMockersTest, MocksFunctionAsLongAsMockInstanceExists) {
22 EXPECT_FUNCTION_CALL(mock, (1, 2)).WillOnce(Return(12));
23 ASSERT_EQ(12, add(1, 2));
26 ASSERT_EQ(3, add(1, 2));
30 * real static mock class field holds pointer to a real function.
32 TEST(FunctionMockersTest, FunctionMockExportsRealFunctionPointer) {
33 EXPECT_EQ(3, AddFunctionMock::real(1, 2));
37 * Function negate doesn't exist, but can be mocked.
39 TEST(FunctionMockersTest, ThrowsExceptionIfRealFunctionNotFound)
42 NegateFunctionMock mock;
44 EXPECT_FUNCTION_CALL(mock, (9)).WillOnce(Return(3));
46 ASSERT_EQ(3, negate(9));
49 EXPECT_THROW(negate(9), std::logic_error);