You can monitor stubs execution to check against certain expectations, such as the number of calls to a given stub or stubs call sequence. Expectations should be defined inside the test cases and are verified just after the test case execution is finished. If the expectation is not met, then a suitable message is generated and output to the results. All expectations are removed at the end of test case execution after the outcome has been verified. The following table describes the available expectation types:
Expectation | Description |
---|---|
CPPTEST_EXPECT_NCALLS(func-id, number) | Defines an expectation for the number of calls to the “func-id” stub to be exactly equal to the specified number. |
CPPTEST_EXPECT_NCALLS_MORE_THAN(func-id,number) | Defines an expectation for the number of calls to the “func-id” stub to be greater than the specified number |
CPPTEST_EXPECT_NCALLS_LESS_THAN(func-id,number) | Defines an expectation for the number of calls to the “func-id” stub to be lower than the specified number |
CPPTEST_EXPECT_NCALLS_IN_RANGE(func-id,min,max) | Defines an expectation for the number of calls to the “func-id” stub to be greater than the "min" and less than the "max" |
CPPTEST_EXPECT_CALL_SEQUENCE() [Add(func-id)|AddNTimes(func-id)] | Defines an expectation for the sequence of calls made to the stubs |
The following section presents most typical examples of expectations definitions for stubs.
Example 1: Number of Stub Calls Expectations
The C and C++ API is the same for defining the stub call number expectations.
[C/C++] CPPTEST_EXPECT_NCALLS("foo",3);
[C/C++] CPPTEST_EXPECT_NCALLS_MORE_THAN("foo",3);
[C/C++] CPPTEST_EXPECT_NCALLS_LESS_THAN("foo",3);
[C/C++] CPPTEST_EXPECT_NCALLS_IN_RANGE("foo",3, 7);
Example 2: Sequence of Stub Calls Expectation
[C++] CPPTEST_EXPECT_CALL_SEQUENCE().Add("foo").Add(goo").
AddNTimes("foo", 3).Add("goo");
[C] CPPTEST_EXPECT_CALL_SEQUENCE()->Add("foo")->Add(goo")->
AddNTimes("foo", 3)->Add("goo");