returnStoredValueWhen

Class: matlab.mock.TestCase
Package: matlab.mock

Return stored property value

Syntax

returnStoredValueWhen(testcase,behavior)

Description

returnStoredValueWhen(testcase,behavior) specifies that the mock returns the stored property value when a property is accessed. If the mock is strict and the property is an abstract property of the mock interface, the framework produces an assertion failure when it accesses a property. To enable access to the property in a strict mock, use the returnStoredValueWhen method.

Input Arguments

expand all

Instance of the test case, specified as a matlab.mock.TestCase object.

Behavior of the mock, specified as a matlab.mock.PropertyGetBehavior instance. To create an instance of matlab.mock.PropertyGetBehavior, call the get method on a property of the behavior object.

Example: get(behavior.MyMockedProperty)

Examples

expand all

Create a strict mock. All property interactions throw exceptions by default.

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

Enable PropertyFoo to be accessed instead of throwing an exception.

testCase.returnStoredValueWhen(get(behavior.PropertyFoo));

Alternatives

Using the returnStoredValueWhen method is functionally equivalent to using the matlab.mock.actions.ReturnStoredValue action with the when method of the PropertyGetBehavior class. For example, the following code blocks are functionally equivalent.

% Using the returnStoredValueWhen method
testCase.returnStoredValueWhen(get(behavior.PropertyFoo));

% Using the ReturnStoredValue action with the when function
import matlab.mock.actions.ReturnStoredValue;
when(get(behavior.PropertyFoo),ReturnStoredValue);
However, there is more functionality when you use the ReturnStoredValue action. For instance, you can specify different subsequent behavior for the same mocked object interaction.

Introduced in R2017a