Conditional Assertion - Based on arguments |
Top Previous Next |
To verify that a method was called with specific arguments, Isolator++ Professional provides Conditional Checkers. These allow you to specify the expected arguments and verify their conditions during method calls.
The conditional API is the same as Conditional Behavior Faking
Note: Conditional asserts only works with value types, as Isolator++ Professional doesn't copy or save other types of argument for this check. Use Custom Assertions for such cases.
Example
Assuming the following class is in the production code
class Person { public: int GetAverageChildAge(int child1, int child2) { return (child1 + child2) / 2; } };
The following code will pass if the method was called with the specific arguments - the first is 2 and the second smaller than 2:
auto a = Isolator(); ... a.CallTo(livePerson->GetAverageChildAge(A::Eq(2),A::Lt(2))).VerifyWasCalled();
Example with private methods with out/by-ref value-type arguments
Use the Ref APIs for arguments passed by reference:
a.CallTo(person->GetOlder(A::EqRef(0))).VerifyWasCalled();
Using the same example as above, assuming GetAverageChildAge is private:
a.CallToPrivate(A::Member(livePerson, GetAverageChildAge),A::Eq(2),A::Lt(2)).VerifyWasCalled();
The following code will pass if the method was called with a specific child name:
a.CallToPrivate(A::Member(livePerson, GetAverageChildAge), A::Matches([](string val) {return val == "Child Name"} )) .VerifyWasCalled();
Example with private methods with out/by-ref value-type arguments
And for methods with out/by-ref value-type arguments you can use same construction:
a.CallToPrivate(A::Member(person, ChangeLastName),A::EqRef("Smith")).VerifyWasCalled();
Creating Custom Conditions
To create custom conditions, use the A::Matches API and implement the condition within the lambda method.
Using the previous code, Let's create a test that will pass only if the method was called with the child1 of age 2 or 5. child2 can be any value:
a.CallTo(livePerson->GetAverageChildAge(A::Matches([](int val){return val==2 || val==5 ;}), A::Any()) .VerifyWasCalled();
Creating Custom Conditions between many arguments
To create a condition that requires testing multiple complex or inter-arguments relationships, use a predicate as shown in Custom Assertions with Predicates. |
Copyright Typemock Ltd. 2009-2025. All Rights Reserved.