Invoke function handle when method is called
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
action = invoke( invokes the function specified
fh when a method is called.
fh— Function to invoke
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
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.
Function— Function to invoke
Function to invoke when a mock object method is called, stored as a function handle.
Value. To learn how value classes affect copy operations, see Copying Objects.
Create a mock for a class that represents a 6-sided die and includes a mocked
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
val = mock.roll
val = 5