Conditional Assertion - based on arguments

Top  Previous  Next

Isolator++ Professional allows you to assert that a method was called with specific arguments.

To do this use the Conditional Checkers in the ASSERT_WAS_CALLED statement. This works with ASSERT_NOT_CALLED and TIMES_CALLED

Note: only public methods are supported, use Custom Assertions with Predicates for private methods.

 

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 this

 

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:

 

 

ASSERT_WAS_CALLED(livePerson->GetAverageChildAge(EQ(2),LT(2)));

 

 

Example with private methods with out/by-ref value-type arguments

Note: You can use BY_REF macro in conditions, instead of passing the pointer itself:

 

 

ASSERT_WAS_CALLED(person->GetOlder(EQ(BY_REF(0))));

 

 

Example with Private Methods

 

Using the same example as above, assuming GetAverageChildAge is private:

 

 

PRIVATE_ASSERT_WAS_CALLED(livePerson, GetAverageChildAge, (EQ(2),LT(2));

 

 

The following code will pass if the method was called with a specific child name:

 

PRIVATE_ASSERT_WAS_CALLED(livePersonGetAverageChildNameIS<std::string>([](std::string& str)

{

 return str == "Child name";

}));

 

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:

 

 

PRIVATE_ASSERT_WAS_CALLED(person, ChangeLastName, EQ(&neededLastName));

 

 

or

 

 

PRIVATE_ASSERT_WAS_CALLED(person, ChangeLastName, EQ("Smith"));

 

 

Creating Custom Conditions

 

To create custom conditions, use the IS macro 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 2 or 5. child2 can be any value (_)

 

 

ASSERT_WAS_CALLED(livePerson->GetAverageChildAge(IS(<int>([](int& i) {return  i==2 || i==5 ;})),_);

 

 

Creating Custom Conditions between many arguments

 

To create a condition that requires testing multiple complex or inter-arguments testing use the predicate as show in Custom Assertions with Predicates


Copyright  Typemock Ltd. 2009-2025.  All Rights Reserved.