Asserting a method was not called |
Top Previous Next |
Each test needs a pass/fail criteria. For testing state (values of the object), you'll use the test framework's Assert APIs. In some cases, you'd want to assert a method has NOT been called during the test.
For asserting an instance method was not called:
ASSERT_NOT_CALLED(person->GetAddress());
To assert instance methods were either called or not, 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 also be asserted:
ASSERT_NOT_CALLED(Person::CreatePerson());
Like in the instance case, for ASSERT_WAS_CALLED to work, we need to use FAKE_STATICS before that. or use a WHEN_CALLED on one of the static methods.
Asserting a method was not called with condition
To assert method wasn't called with exact arguments you can use a predicate:
ASSERT_NOT_CALLED(person->IsPartOfGroup(EQ(2)));
Asserting a methods with out/by-ref value-type was not called with condition
int age = 2; ASSERT_NOT_CALLED(person->GetOlder(EQ(&age)));
Note: You can use BY_REF macro in conditions, instead of passing the pointer itself:
ASSERT_NOT_CALLED(person->GetOlder(EQ(BY_REF(2))));
Asserting Private Methods For private methods use the PRIVATE_ASSERT_NOT_CALLED API. For asserting an instance method was called:
PRIVATE_ASSERT_NOT_CALLED(person, GetAddress);
Asserting private static methods In a similar fashion, a static method can be asserted:
PRIVATE_ASSERT_NOT_CALLED(person, Person::GetAverageAge);
Asserting a method was not called with condition 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
PRIVATE_ASSERT_NOT_CALLED(person, SaveAll, IS<std::string>([](std::string& val) {return val == "foo"} ));
For more information about conditional assertion click here.
Asserting private methods with out/by-ref value-type And for private methods with out/by-ref value-type using the same macros:
PRIVATE_ASSERT_NOT_CALLED(person, ChangeLastName, EQ(&randomLastName));
or
PRIVATE_ASSERT_NOT_CALLED(person, ChangeLastName, EQ("Smith"));
You can find more information about faking methods with out/by ref value-type arguments here.
Asserting overloaded private methods To specify an overloaded method by passing the types of the arguments using TYPEOF(Type) macro.
PRIVATE_ASSERT_NOT_CALLED(person, CanPing, TYPEOF(bool));
|
Copyright Typemock Ltd. 2009-2025. All Rights Reserved.