repeat

Class: matlab.mock.actions.AssignOutputs
Package: matlab.mock.actions

Repeat defining return values

Syntax

repeat(n,action)

Description

repeat(n,action) repeats the same action multiple times.

Input Arguments

expand all

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

Example: 5

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

Example: AssignOutputs(true)

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.AssignOutputs
when(withExactInputs(behavior.isOpen),then(repeat(2,AssignOutputs(true)), ...
    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),then(repeat(2,AssignOutputs(true))));
    for i = 1:4
        tf = mock.IsJointAccount
    end
    

    But the following code returns true twice and false twice.

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

Introduced in R2017a