then

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

Specify action

Syntax

then(action1)
then(action1,action2)

Description

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.ThrowException.

Example: StoreValue

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

Examples

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),then(StoreValue,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>mockPropertySetCallback (line 706)
Value set too many times.

Error in matlab.mock.internal.MockContext>@(name,obj,value)mockPropertySetCallback(name,obj,value,catalog)
(line 284)
            propertySetCallback = @(name, obj, value)mockPropertySetCallback(name, obj, value, catalog);

Tips

  • 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 then(action1,then(action2,action3)).

Introduced in R2017a