Main Content

then

Class: matlab.mock.actions.Invoke
Namespace: matlab.mock.actions

Action for mock object interaction or action subsequent to invoking function handle

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

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

Examples

expand all

Create a mock for a class that represents a 6-sided die, including a mocked roll method.

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

Set up behavior for the roll method. Return 0 the first time it runs. On subsequent runs, invoke the randi function to return a random integer between 1 and 6.

import matlab.mock.actions.AssignOutputs
import matlab.mock.actions.Invoke
when(withExactInputs(behavior.roll), ...
    AssignOutputs(0).then(Invoke(@(~)randi(6))))

Call the mocked roll method three times.

val = mock.roll
val = 0
val = mock.roll
val = 5
val = mock.roll
val = 6

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

Version History

Introduced in R2018b