Constraint qualifying mock object interactions
Occurred constraint qualifies the occurrence of one or more mock object
interactions. It produces a qualification failure for any actual-value array that specifies at
least one interaction that did not occur. The actual value must be an array of
PropertySetBehavior objects that all refer to the same mock object.
Occurred constraint to qualify any combination of method calls,
property accesses, or property modifications.
By default, the constraint qualifies that all interactions occurred at least once and in
any order. The
RespectingOrder name-value pair enables qualification that
the interactions occurred in the specified order.
constraint = matlab.mock.constraints.Occurred provides a constraint that
determines if all specified interactions occurred.
constraint = matlab.mock.constraints.Occurred('RespectingOrder',
provides a constraint that respects the order of occurrence of the specified
tf — Respect order of interactions
false (default) |
Whether to respect the order of interactions, specified as
true. By default, the constraint does
not require that interactions occur in a specified order.
RespectOrder — Respects order of interactions
false (default) |
This property is read-only.
Whether the constraint respects the order of interactions, stored as
RespectOrder property is
false by default, but
can be set to
true during construction of the constraint by using the
'RespectingOrder' name-value pair.
Value. To learn how value classes affect copy operations, see Copying Objects.
Qualify Mock Interactions
Create a mock for a person class.
testCase = matlab.mock.TestCase.forInteractiveUse; [fakePerson,behavior] = testCase.createMock("AddedProperties",["Name","Age"], ... "AddedMethods","speak");
Use the mock by calling the
speak method, accessing the
Age property, and setting the
fakePerson.speak("hello"); age = fakePerson.Age; fakePerson.Name = "Zed";
Verify that a call to the
speak method with the input
import matlab.mock.constraints.Occurred; testCase.verifyThat(behavior.speak("hello"),Occurred)
Verify that the
Age property was accessed, the
speak method was called with
"hello", and the
Name property was set to
testCase.verifyThat([get(behavior.Age), ... behavior.speak("hello"), ... behavior.Name.setToValue("Zed")],Occurred)
Repeat the verification but require the constraint is satisfied only if the
interactions occurred in the specified order. This test fails because the
speak method was called before the
testCase.verifyThat([get(behavior.Age), ... behavior.speak("hello"), ... behavior.Name.setToValue("Zed")],Occurred('RespectingOrder',true))
Verification failed. --------------------- Framework Diagnostic: --------------------- Occurred failed. --> All specified interactions occurred. --> The interactions did not occur in the specified order. Actual order: speak([1×1 matlab.mock.classes.Mock], "hello") <Mock>.Age <Mock>.Name = "Zed" Specified interactions: 1×3 heterogeneous InteractionBehavior (PropertyGetBehavior, MethodCallBehavior, PropertySetBehavior) array with no properties. <Mock>.Age [...] = speak(<Mock>, "hello") <Mock>.Name = "Zed"
Repeat the verification and specify interactions should occur in a different order.
testCase.verifyThat([behavior.speak("hello"), ... get(behavior.Age), ... behavior.Name.setToValue("Zed")],Occurred('RespectingOrder',true))