Main Content


Class: matlab.mock.actions.StoreValue
Package: matlab.mock.actions

Action for mock object interaction or action subsequent to storing property value




then(action1) specifies an action for mock object interactions.

then(action1,action2) specifies an action and a subsequent action for mock object interactions.

Input Arguments

expand all

Defined action, specified as an instance of matlab.mock.actions.StoreValue.

Second defined action, specified as an instance of matlab.mock.actions.DoNothing or matlab.mock.actions.ThrowException.

Example: action2 = DoNothing

Example: action2 = ThrowException(MException('Account:deposit:Negative','Deposit amount must be positive.'))


expand all

Create a mock for a bank account class.

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

Specify behavior. Allow the IsOpen property to be set once, and then throw an exception.

import matlab.mock.actions.StoreValue
import matlab.mock.actions.ThrowException
when(set(behavior.IsOpen),StoreValue().then(ThrowException( ...
    MException('Account:setValue:tooMany','Value set too many times.'))))

Use the mock.

mock.IsOpen = false
mock = 

  Mock with properties:

    IsOpen: 0
mock.IsOpen = true
Error using matlab.mock.internal.MockContext/createMockObject/mockPropertySetCallback (line 429)
Value set too many times.


  • Each call to then accepts up to two actions. To specify more subsequent actions, use multiple calls to then. For example, to specify three actions, use action1.then(action2).then(action3).

Version History

Introduced in R2017a