Asserting a method was called |
Top Previous Next |
Each test needs a pass/fail criteria. For testing state (values exposed by the object), you can use the test framework's Assert API's. But in some cases, you want to assert a method was called - to test the interaction between the tested code and its dependency. For this, you'll use the ASSERT_WAS_CALLED API.
For asserting an instance method was called:
ASSERT_WAS_CALLED(person->GetAddress());
To assert instance methods, person needs to be either declared with FAKE, or a live object, with at least one WHEN_CALLED setting.
In a similar fashion, a static method can be asserted:
ASSERT_WAS_CALLED(Person::GetAverageAge());
Like in the instance case, for ASSERT_WAS_CALLED to work, we need to use FAKE_STATICS or FAKE_GLOBAL before that, or use a WHEN_CALLED on one of the static methods.
If the method has parameters, the parameter values are ignored: You can use of the ignored parameters methods to tell Isolator++ Professional.
ASSERT_WAS_CALLED(Person::CreatePerson(_));
In order to perform specific assertion, based on arguments, check Custom Assertions.
Asserting Private Methods For asserting an instance method was called:
PRIVATE_ASSERT_WAS_CALLED(person,GetAddress);
In a similar fashion, a static method can be asserted:
PRIVATE_ASSERT_WAS_CALLED(_,Person::GetAverageAge);
Asserting overloaded private methods To specify an overloaded method by passing the types of the arguments using TYPEOF(Type) macro. To assert overloaded private method based on arguments, check Custom Assertions.
PRIVATE_ASSERT_WAS_CALLED(person, CanPing, TYPEOF(bool));
|
Copyright Typemock Ltd. 2009-2025. All Rights Reserved.