If you are having trouble getting tests running reproducibly, you might need to use a "scrubber" to convert the non-deterministic text to something stable.
Fundamentally, a scrubber is function that takes a string and returns a string. You can create ones by passing in a function or a lambda. We also have some pre-made ones for your convenience.
Approvals::verify(
"1 2 3 4 5 6",
Options().withScrubber(
[](const std::string& t) {return StringUtils::replaceAll(t, "3", "Fizz");}
));
This would produce:
1 2 Fizz 4 5 6
(In the real world, scrubbers are generally used to remove text that is expected to differ between test runs... Here, we use a trivial example for ease of explanation.)
You can scrub GUIDs by using a pointer to the function Scrubbers::scrubGuid
, for example the following code:
std::string jsonFromRestCall = R"(
{
child: {
id: b34b4da8-090e-49d8-bd35-7e79f633a2ea
parent1: 2fd78d4a-ad49-447d-96a8-deda585a9aa5
parent2: 05f77de3-3790-4d45-b045-def96c9cd371
}
person: {
name: mom
id: 2fd78d4a-ad49-447d-96a8-deda585a9aa5
}
person: {
name: dad
id: 05f77de3-3790-4d45-b045-def96c9cd371
}
}
)";
Approvals::verify(jsonFromRestCall, Options().withScrubber(Scrubbers::scrubGuid));
will produce:
{
child: {
id: guid_1
parent1: guid_2
parent2: guid_3
}
person: {
name: mom
id: guid_2
}
person: {
name: dad
id: guid_3
}
}
Notice that when GUIDs are repeated within the same file, they are replaced with the same text.