Main Content

then

Class: matlab.mock.actions.ThrowException
Namespace: matlab.mock.actions

Action for mock object interaction or action subsequent to throwing exception

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

Example: action1 = ThrowException

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

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

Example: action2 = ThrowException

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

Examples

expand all

Create a mock for a bank account class.

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

Specify behavior.

import matlab.mock.actions.ThrowException
import matlab.mock.actions.AssignOutputs
when(withExactInputs(behavior.isOpen), ...
    ThrowException().then(AssignOutputs(false)))

Use the mock.

isAccountOpen = mock.isOpen

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

Use the mock.

isAccountOpen = mock.isOpen
isAccountOpen =

  logical

   0

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 R2017a