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

 

API

Meaning

Example

A::Any

Matches any argument

a.CallTo(fake->Foo(A::Any()).VerifyWasCalled();

A::Type<type>

Argument must be of the specified type

a.CallTo(fake->Foo(A::Type<char>()).VerifyWasCalled();

A::Eq and  A::EqRef

Argument must equal a value

a.CallTo(fake->Foo(A::Eq(100))).VerifyWasCalled();

A::Ne and A::NeRef

Argument must not equal a value

a.CallTo(fake->Foo(A::Ne(3))).VerifyWasCalled();

A::Lt and A::LtRef

Argument is smaller than the value

a.CallTo(fake->Foo(A::Lt(5))).VerifyWasCalled();

A::Le and A::LeRef

Argument is smaller or equals

a.CallTo(fake->Foo(A::Le(4))).VerifyWasCalled();

A::Gt and A::GtRef

Argument is greater than the value

a.CallTo(fake->Foo(A::Gt(10.2))).VerifyWasCalled();

A::Ge and A::GeRef

Argument is greater or equals

a.CallTo(fake->Foo(A::Ge(1))).VerifyWasCalled();

A::Matches and A::MatchesRef

Matches a custom lambda condition

a.CallTo(fake->Foo(A::Matches([](char* s) 

  { return  !strcmp(s, "typemock"); }))

  .VerifyWasCalled();

 

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();  

 

 

Example with Private Methods

 

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.