Main Content

matlab.mock.AnyArguments 类

命名空间: matlab.mock

匹配任意数量的参数

描述

在指定 mock 行为或限定 mock 交互时,可使用 AnyArguments 类,以匹配任何数量的参数。

构造

AnyArguments 可匹配不受限制的、数量不定的参数,包括零。在定义 mock 行为或限定 mock 交互时,将 AnyArguments 指定为参数列表中的最后一个参数。

复制语义

值。要了解值类如何影响复制操作,请参阅复制对象

示例

全部折叠

匹配任意数量的参数。

import matlab.mock.AnyArguments
import matlab.mock.actions.ThrowException

testCase = matlab.mock.TestCase.forInteractiveUse;

% Create a mock for a bank account class
[saboteurAccount,behavior] = testCase.createMock('AddedMethods',"deposit");

% Define behavior to throw exception with any input argument
when(behavior.deposit(AnyArguments),ThrowException)

% All of the following interactions throw an exception:
saboteurAccount.deposit;
saboteurAccount.deposit(-10);
saboteurAccount.deposit(10);
saboteurAccount.deposit('a','b','c');

备选方法

AnyArguments 类在功能上与使用 matlab.mock.MethodCallBehavior 类的 withAnyInputs 方法类似。例如,以下代码块在功能上相似。

% Using the AnyArguments class
import matlab.mock.AnyArguments;
testCase.verifyCalled(behavior.myMethod(AnyArguments));

% Using the withAnyInputs method
testCase.verifyCalled(withAnyInputs(behavior.myMethod))
但是,AnyArguments 需要 mock 是第一个输入参数,而 withAnyInputs 不需要。MethodCallBehavior 类还提供了一些其他方法用来指定行为和记录交互,例如,指定确切的输入或许多输出。

版本历史记录

在 R2017a 中推出