CppUTest documentations says
MockSupportPlugin makes the work with mocks easier. It does the following work for you automatically:
- checkExpectations at the end of every test (on global scope, which goes recursive over all scopes)
- clear all expectations at the end of every test
- install all comparators that were configured in the plugin at the beginning of every test
- remove all comparators at the end of every test
ref: https://cpputest.github.io/plugin_manual.html
I tried the following example:
#include "CppUTest/TestRegistry.h"
#include "CppUTestExt/MockSupportPlugin.h"
MyDummyComparator dummyComparator;
MockSupportPlugin mockPlugin;
mockPlugin.installComparator("MyDummyType", dummyComparator);
TestRegistry::getCurrentRegistry()->installPlugin(&mockPlugin);
with my added MYDummyComparator:
class MyDummyComparator : public MockNamedValueComparator
{
bool isEqual( const void *object1, const void *object2 )
{
return object1 == object2;
}
SimpleString valueToString( const void *object )
{
return SimpleString();
}
} dummyComparator;
But when I remove expectOneCall() or expectNCalls() from my tests, it shows the tests failed. How do I use MockSupportPlugin from CPPUTest to achieve doing "checkExpectations at the end of every test (on global scope, which goes recursive over all scopes)" automatically?
The mock type comparators would be used in your mock comparisons.
For example, you need to compare a struct of type Point
, which looks like this:
struct Point {
int x;
int y;
};
You would define your comparator like this:
class PointTypeComparator : public MockNamedValueComparator
{
public:
bool isEqual(const void* object1, const void* object2) override
{
// Casting here the void pointers to the type to compare
const auto *pointObject1 = (const Point *) object1;
const auto *pointObject2 = (const Point *) object2;
// Perform comparison, in this case, comparing x and y
return ((pointObject1->x == pointObject2->x)
&& (pointObject1->y == pointObject2->y);
}
virtual SimpleString valueToString(const void* object)
{
return (char *) "string";
}
};
Next, within you test group, you need to install these comparators in the setup
and also in the teardown
clear them:
TEST_GROUP(MyTest)
{
void setup()
{
PointTypeComparator pointComparator;
mock().installComparator("Point *", pointComparator); // Note, its a pointer to a Point type
}
void teardown()
{
// Call check expectations here, and also clear all comparators after that
mock().checkExpectations();
mock().clear();
mock().removeAllComparatorsAndCopiers();
}
};
Next, you can use this Comparator, using the withParameterOfType
function as:
mock().expectOneCall("foo")
.withParameterOfType("Point *", "name", &address); // Here name is the name of variable, and &address is the address of the Point type variable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.