Main Content

repeat

Class: matlab.mock.actions.AssignOutputs
Namespace: matlab.mock.actions

Repeat defining return values

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

Example: action = AssignOutputs(true)

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

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.AssignOutputs
when(withExactInputs(behavior.isOpen), ...
    AssignOutputs(true).repeat(2).then(AssignOutputs(false)))

Use the mock.

for i = 1:3
    isAccountOpen = mock.isOpen
end
isAccountOpen = logical
   1

isAccountOpen = logical
   1

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.AssignOutputs
    testCase = matlab.mock.TestCase.forInteractiveUse;
    [mock,behavior] = testCase.createMock('AddedProperties',"IsJointAccount");

    If you repeat an action to return a property value of true twice, the following code, which goes on to get the property value a third and fourth time, returns true all four times.

    when(get(behavior.IsJointAccount),AssignOutputs(true).repeat(2))
    for i = 1:4
        tf = mock.IsJointAccount
    end
    

    But the following code returns true twice and false twice.

    when(get(behavior.IsJointAccount), ...
        AssignOutputs(true).repeat(2).then(AssignOutputs(false)))
    for i = 1:4
        tf = mock.IsJointAccount
    end

Version History

Introduced in R2017a