Main Content

when

Class: matlab.mock.MethodCallBehavior
Namespace: matlab.mock

Specify mock object method behavior

Syntax

when(behavior,action)

Description

when(behavior,action) specifies the action that a mock object method takes when it is called with the inputs defined by behavior.

Input Arguments

expand all

Behavior of the mock, specified as a matlab.mock.MethodCallBehavior instance. To create an instance of matlab.mock.MethodCallBehavior, call a method of the behavior object.

Example: withExactInputs(myMockBehavior.myMockedMethod)

Defined action, specified as an instance of matlab.mock.actions.AssignOutputs, matlab.mock.actions.Invoke, matlab.mock.actions.DoNothing, or matlab.mock.actions.ThrowException.

Example: AssignOutputs(7,13,42)

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

Examples

expand all

Create a mock for a triangle class. The mock has one method, sideLengths.

import matlab.mock.actions.AssignOutputs;
testCase = matlab.mock.TestCase.forInteractiveUse;
[mock,behavior] = testCase.createMock('AddedMethods',"sideLengths");

Set up behavior. Regardless of the inputs to the sideLengths method, the mock returns the values 2, 3, and 4.

when(withAnyInputs(behavior.sideLengths),AssignOutputs(2,3,4))

Call the sideLengths method of the mock object.

[a,b,c] = mock.sideLengths
a = 2
b = 3
c = 4

Call the sideLengths method again using different inputs and only two outputs.

[a,b] = mock.sideLengths(13,"inputText")
a = 2
b = 3

Version History

Introduced in R2017a