Main Content

matlab.mock.AnyArguments Class

Namespace: matlab.mock

Match any number of arguments

Description

Use the AnyArguments class to match any number of arguments when specifying mock behavior or qualifying mock interactions.

Construction

AnyArguments matches an unlimited, unspecified number of arguments, including zero. When defining mock behavior or qualifying mock interactions, specify AnyArguments as the last argument in the argument list.

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects.

Examples

collapse all

Match any number of arguments.

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');

Alternatives

The AnyArguments class is functionally similar to using the withAnyInputs method of the matlab.mock.MethodCallBehavior class. For example, the following code blocks are similar.

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

% Using the withAnyInputs method
testCase.verifyCalled(withAnyInputs(behavior.myMethod))
However, AnyArguments requires that the mock is the first input argument, and withAnyInputs does not. The MethodCallBehavior class provides additional methods to specify behavior and record interactions, such as specification of exact inputs or a number of outputs.

Version History

Introduced in R2017a