storeValueWhen

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

Store property value

Syntax

storeValueWhen(testcase,behavior)

Description

storeValueWhen(testcase,behavior) specifies that the mock should store the property value when a property is set. If the mock is strict and the property is an abstract property of the mock interface, the framework produces an assertion failure at property set access. To enable the property to be set in a strict mock, use the storeValueWhen 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.PropertySetBehavior instance. To create an instance of matlab.mock.PropertySetBehavior, call the set method on a property of the behavior object.

Example: set(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 set instead of throwing an exception.

testCase.storeValueWhen(set(behavior.PropertyFoo));

Alternatives

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

% Using the storeValueWhen method
testCase.storeValueWhen(set(behavior.PropertyFoo));

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

Introduced in R2017a