Main Content

matlab.mock.actions.ReturnStoredValue class

Package: matlab.mock.actions

Return stored property value

Description

The ReturnStoredValue action specifies that the stored value is returned when accessing a property.

By default, when you access property values in strict mocks, MATLAB® throws an exception. To specify that the framework returns the stored value of a mock object property, use the ReturnStoredValue class.

Construction

action = ReturnStoredValue returns the stored property value.

Methods

repeatRepeat returning stored property value
thenAction for mock object interaction or action subsequent to returning stored property value

Copy Semantics

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

Examples

collapse all

Create a strict mock. By default, property interactions throw exceptions.

testCase = matlab.mock.TestCase.forInteractiveUse;
properties = ["PropA" "PropB" "PropC"];
[mock,behavior] = testCase.createMock('AddedProperties',properties,"Strict",true);

Enable access to PropA.

import matlab.mock.actions.ReturnStoredValue
when(get(behavior.PropA),ReturnStoredValue)

Use the mock.

val1 = mock.PropA
val1 =

     []
val2 = mock.PropB
Assertion failed.
    ----------------
    Test Diagnostic:
    ----------------
    Unexpected access of strict mock property 'PropB'.
Assertion failed.

Alternatives

For a simple directive to return property values, you can use the returnStoredValueWhen method of the matlab.mock.TestCase class. However, there is more functionality when you use the ReturnStoredValue action.

Version History

Introduced in R2017a