Asserting a method was not called |
Top Previous Next |
In addition to testing state with Assert APIs, there are cases where you'd want to assert that a method was not invoked during the test. Isolator++ Professional provides the VerifyWasNotCalled API to achieve this.
To assert an instance method was not called:
auto a = Isolator(); ... a.CallTo(person->GetAddress()).VerifyWasNotCalled();
For this to work, person needs to be either declared with a.Fake.Instance, or as a live object with at least one CallTo setting.
In a similar fashion, a static method can also be asserted:
a.CallTo(Person::CreatePerson()).VerifyWasNotCalled();
Like in the instance case, for VerifyWasNotCalled to work, we need to use a.Fake.Statics or a.Fake.GlobalMethod beforehand or use a CallTo on one of the static methods.
Asserting a method was not called with condition
To assert that a method was not called with specific arguments, you can use argument matching APIs like A::Eq, A::EqRef, or A::Matches.
a.CallTo(person->IsPartOfGroup(A:Eq(2))).VerifyWasNotCalled();
Asserting a method with in-out/by-ref value-type was not called with condition
a.CallTo(person->GetOlder(A:EqRef(2))).VerifyWasNotCalled();
Asserting Private Methods For private methods use the CallToPrivate API with VerifyWasNotCalled. To assert an instance method was called:
a.CallToPrivate(A::Member(person, GetAddress)).VerifyWasNotCalled();
Asserting private static methods In a similar fashion, a static method can be asserted:
a.CallToPrivate(A::Global(Person::GetAverageAge)).VerifyWasNotCalled();
Asserting a method was not called with condition Note: Conditional asserts only work with value types, as Isolator++ Professional doesn't copy or save other types of argument for this check. Use Conditional Assertions for this:
a.CallToPrivate(A::Member(person, SaveAll),A::Matches([](string val) {return val == "foo"} )) .VerifyWasNotCalled();
For more information about conditional assertion, click here.
Asserting private methods with out/by-ref value-type For private methods with out/by-ref value-type arguments:
a.CallToPrivate(A::Member(person, ChangeLastName),A::EqRef("Smith"))).VerifyWasNotCalled();
You can find more information about faking methods with out/by ref value-type arguments here.
Asserting overloaded private methods To assert that an overloaded private method was not called, specify argument types using A::Type<type>:
a.CallToPrivate(A::Member(person, CanPing),A::Type<bool>())).VerifyWasNotCalled();
Here is an example:
TEST_F(Examples, VerifyingCalls) { // Arrange auto a = Isolator(); auto fake = a.Fake.Instance<TestPrivate>(FakeOptions::CallOriginal);
// Act fake->SetPrivate(2);
// Assert a.CallToPrivate(A::Member(fake, PrivateMethod),A::Type<PCSTR>(),A::Type<int>())).VerifyWasNotCalled(); a.CallToPrivate(A::Member(fake, PrivateMethod))).VerifyWasNotCalled();
a.CallToPrivate(A::Member(fake, PrivateMethod),A::Type<int>())).VerifyWasCalled();
} |
Copyright Typemock Ltd. 2009-2025. All Rights Reserved.