Main Content

repeat

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

Repeat throwing exception

Syntax

repeat(action,n)

Description

repeat(action,n) repeats the same action n times. You can specify the input arguments in any order. That is, repeat(action,n) and repeat(n,action) both repeat the action n times.

Input Arguments

expand all

Defined action, specified as an instance of matlab.mock.actions.ThrowException.

Example: action = ThrowException

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

Number of times to repeat the action, specified as an integer.

Example: 5

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().repeat(2).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])
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])
isAccountOpen = mock.isOpen
isAccountOpen =

  logical

   0

Tips

  • If you repeat an action, and do not follow it with a call to the then method, the mock continues to return the repeated value. For example, consider the following mock of a bank account class.

    import matlab.mock.actions.ThrowException
    testCase = matlab.mock.TestCase.forInteractiveUse;
    [mock, behavior] = testCase.createMock('AddedProperties',"IsJointAccount");

    If you repeat an action to throw an exception twice, the framework continues to throw an exception in the following code, which goes on to get the property value a third time.

    when(get(behavior.IsJointAccount),ThrowException().repeat(2))
    tf = mock.IsJointAccount
    tf = mock.IsJointAccount
    tf = mock.IsJointAccount

    But the following code throws an exception twice and the returns false.

    import matlab.mock.actions.AssignOutputs
    when(get(behavior.IsJointAccount), ...
        ThrowException().repeat(2).then(AssignOutputs(false)))
    tf = mock.IsJointAccount
    tf = mock.IsJointAccount
    tf = mock.IsJointAccount

Version History

Introduced in R2017a