Main Content

then

Class: matlab.mock.actions.DoNothing
Namespace: matlab.mock.actions

Action for mock object interaction or action subsequent to taking no action

Since R2020a

Description

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

example

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

Input Arguments

expand all

Defined action, specified as a matlab.mock.actions.DoNothing object.

Example: action1 = DoNothing

Second defined action, specified as a matlab.mock.actions.AssignOutputs, matlab.mock.actions.Invoke, matlab.mock.actions.StoreValue, or matlab.mock.actions.ThrowException object.

Example: action2 = AssignOutputs(7,13,42)

Example: action2 = ThrowException

Examples

expand all

Create a mock for a bank account class.

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

Do nothing the first time deposit is called. Throw an exception when the method is called a second time.

import matlab.mock.actions.DoNothing
import matlab.mock.actions.ThrowException
when(withAnyInputs(behavior.deposit),DoNothing().then(ThrowException))

Use the mock. The method call results in no action.

mock.deposit(100)

Call the mock method a second time. MATLAB® throws an error.

mock.deposit(100)
Error using matlab.mock.internal.MockContext/createMockObject/mockMethodCallback (line 382)
The following method call was specified to throw an exception:
	deposit([1×1 matlab.mock.classes.Mock], 100)

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 R2020a