Asserting a method was called |
Top Previous Next |
Tests require clear pass/fail criteria. For state-based testing (e.g., validating values exposed by an object), use the test framework's Assert APIs. However, when testing interactions between the tested code and its dependencies, you can use Isolator++ Professional's VerifyWasCalled API. This allows you to assert that a method was invoked as expected during execution.
To assert an instance method was called:
auto a = Isolator(); ... a.CallTo(person->GetAddress()).VerifyWasCalled();
To assert instance methods, person needs to be either declared with a.Fake.Instance, or as a live object, with at least one CallTo setting.
Similarly, a static method can be asserted:
a.CallTo(Person::GetAverageAge()).VerifyWasCalled();
Like in the instance case, for VerifyWasCalled to work, we need to use a.Fake.Statics or a.Fake.GlobalMethod beforehand, or use a CallTo on one of the static methods.
If the method has parameters, the parameter values can be checked or ignored: To ignore parameters methods use A::Any().
a.CallTo(Person::CreatePerson(A::Any()).VerifyWasCalled();
In order to perform a specific assertion, based on arguments, check Conditional Assertions.
Asserting Private Methods To assert an instance method was called:
a.CallToPrivate(A::Member(&person, GetAddress)).VerifyWasCalled();
Similarly, a static method can be asserted:
a.CallToPrivate(A::Global(Person::GetAverageAge)).VerifyWasCalled();
Asserting overloaded private methods For overloaded methods, you can specify argument types using A::Type<type>. To assert overloaded private methods based on argument values, refer to Conditional Assertions.
a.CallToPrivate(A::Member(&person, CanPing),A::Type<bool>()).VerifyWasCalled();
|
Copyright Typemock Ltd. 2009-2025. All Rights Reserved.