%% Invoke Function Handle at Method Call Time
% 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

% Copyright 2019 The MathWorks, Inc.