Main Content

when

类: matlab.mock.MethodCallBehavior
命名空间: matlab.mock

指定 mock 对象方法的行为

语法

when(behavior,action)

说明

when(behavior,action) 指定当使用 behavior 定义的输入调用 mock 对象方法时,该方法采取的操作。

输入参数

全部展开

mock 的行为,指定为 matlab.mock.MethodCallBehavior 实例。要创建 matlab.mock.MethodCallBehavior 实例,请调用行为对象的方法。

示例: withExactInputs(myMockBehavior.myMockedMethod)

定义的动作,指定为 matlab.mock.actions.AssignOutputsmatlab.mock.actions.Invokematlab.mock.actions.DoNothingmatlab.mock.actions.ThrowException 的实例。

示例: AssignOutputs(7,13,42)

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

示例

全部展开

为 triangle 类创建一个 mock。该 mock 有一个方法,即 sideLengths

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

设置其行为。无论 sideLengths 方法的输入是什么,该 mock 都返回值 2、3 和 4。

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

调用 mock 对象的 sideLengths 方法。

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

使用不同的输入再次调用 sideLengths 方法,这次只使用两个输出。

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

版本历史记录

在 R2017a 中推出