matlab.mock.actions.Invoke class

Package: matlab.mock.actions

Invoke function handle when method is called

Description

To specify that the framework invokes a function handle to determine outputs when a mock object method is called, use the Invoke class. This action differs from the AssignOutputs action, which returns values that are defined when you create the AssignOutputs instance.

Construction

action = invoke(fh) invokes the function specified by fh when a method is called.

Input Arguments

expand all

Function to invoke when a mock object method is called, specified as a function handle.

The framework passes the function handle the same inputs as it passes to the mock method. Therefore, fh typically contains an argument list. If the function does not interact with the mock, for example by accessing a property, you can use tilde (~) in the argument list to ignore the mock object. The framework requests the same number of outputs as the mock method call.

Example: @(~)randi(6)

Example: @(cmock)myFunction('hello')

Example: @isempty

Properties

expand all

Function to invoke when a mock object method is called, stored as a function handle.

Methods

repeatRepeat same action multiple times
thenSpecify action

Copy Semantics

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

Examples

collapse all

Create a mock for a class that represents a 6-sided die and includes a mocked roll method.

testCase = matlab.mock.TestCase.forInteractiveUse;
[mock,behavior] = testCase.createMock("AddedMethods","roll");

Define behavior to return a random integer 1 through 6 each time the roll method is called.

import matlab.mock.actions.Invoke
when(withExactInputs(behavior.roll),Invoke(@(~)randi(6)));

Call the mocked roll method.

val = mock.roll
val = 5

Introduced in R2018b