This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

matlab.mock.constraints.Occurred class

Package: matlab.mock.constraints
Superclasses:

Constraint qualifying mock object interactions

Description

The 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 MethodCallBehavior, PropertyGetBehavior, or PropertySetBehavior objects that all refer to the same mock object.

Use the 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.

Construction

constraint = matlab.mock.constraints.Occurred provides a constraint that determines if all specified interactions occurred.

constraint = matlab.mock.constraints.Occurred('RespectingOrder',tf) provides a constraint that respects the order of occurrence of the specified interactions.

Input Arguments

expand all

Whether to respect the order of interactions, specified as false or true. By default, the constraint does not require that interactions occur in a specified order.

Data Types: logical

Properties

expand all

This property is read-only.

Whether the constraint respects the order of interactions, stored as false or true. The RespectOrder property is false by default, but can be set to true during construction of the constraint by using the 'RespectingOrder' name-value pair.

Data Types: logical

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects.

Examples

collapse all

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 Name property.

fakePerson.speak("hello");
age = fakePerson.Age;
fakePerson.Name = "Zed";

Verify that a call to the speak method with the input "hello" occurred.

import matlab.mock.constraints.Occurred;
testCase.verifyThat(behavior.speak("hello"),Occurred)
Verification passed.

Verify that the Age property was accessed, the speak method was called with "hello", and the Name property was set to "Zed".

testCase.verifyThat([get(behavior.Age), ...
    behavior.speak("hello"), ...
    behavior.Name.setToValue("Zed")],Occurred)
Verification passed.

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 Age property was set.

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))
Verification passed.

Introduced in R2018b