1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
/* Additional include from CppUTestExt */
#include "CppUTest/TestHarness.h"
#include "CppUTestExt/MockSupport.h"
/* Stubbed out product code using linker, function pointer, or overriding */
static int foo(const char* param_string, int param_int)
{
/* Tell CppUTest Mocking what we mock. Also return recorded value */
return mock().actualCall("Foo")
.withParameter("param_string", param_string)
.withParameter("param_int", param_int)
.returnValue().getIntValue();
}
static void bar(double param_double, const char* param_string)
{
mock().actualCall("Bar")
.withParameter("param_double", param_double)
.withParameter("param_string", param_string);
}
/* Production code calls to the methods we stubbed */
static int productionCodeFooCalls()
{
int return_value;
return_value = foo("value_string", 10);
(void)return_value;
return_value = foo("value_string", 10);
return return_value;
}
static void productionCodeBarCalls()
{
bar(1.5, "more");
bar(1.5, "more");
}
/* Actual test */
TEST_GROUP(MockCheatSheet)
{
void teardown()
{
/* Check expectations. Alternatively use MockSupportPlugin */
mock().checkExpectations();
mock().clear();
}
};
TEST(MockCheatSheet, foo)
{
/* Record 2 calls to Foo. Return different values on each call */
mock().expectOneCall("Foo")
.withParameter("param_string", "value_string")
.withParameter("param_int", 10)
.andReturnValue(30);
mock().expectOneCall("Foo")
.ignoreOtherParameters()
.andReturnValue(50);
/* Call production code */
productionCodeFooCalls();
}
TEST(MockCheatSheet, bar)
{
/* Expect 2 calls on Bar. Check only one parameter */
mock().expectNCalls(2, "Bar")
.withParameter("param_double", 1.5)
.ignoreOtherParameters();
/* And the production code call */
productionCodeBarCalls();
}
|