then

Class: matlab.mock.actions.ThrowException
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.ThrowException.

Example: ThrowException

Example: 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.Invoke, matlab.mock.actions.ReturnStoredValue, or matlab.mock.actions.StoreValue.

Example: ThrowException

Example: 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),then(ThrowException,AssignOutputs(false)));

Use the mock.

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

Error in matlab.mock.internal.MockContext>@(data)mockMethodCallback(data,catalog) (line 282)
            methodCallback = @(data)mockMethodCallback(data, catalog);

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

Introduced in R2017a