Main Content


Class: matlab.mock.InteractionHistory
Namespace: matlab.mock

Return history from mock object



history = matlab.mock.InteractionHistory.forMock(mock) returns the history from a mock object. history is an array of matlab.mock.InteractionHistory objects. Each element in history corresponds to one method call, property access, or property modification. The array elements are ordered, with the first element indicating the first recorded interaction. This method returns interactions with publicly visible methods and properties only. For example, the following interactions are not recorded:

  • Calls to Hidden methods

  • Calls to Sealed superclass methods

  • Accesses or modifications of concrete superclass properties

Input Arguments

expand all

Mock to return history of interactions, specified as a mock object.


expand all

Construct a mock with a computeValue method and two properties. Assign a default value of false to Prop2.

tc = matlab.mock.TestCase.forInteractiveUse;
[mock,behavior] = tc.createMock( ...
    'AddedMethods',{'computeValue'}, ...
    'AddedProperties',{'Prop1','Prop2'}, ...

Set up the behavior of the computeValue method to return the value 42, regardless of input values.

import matlab.mock.actions.AssignOutputs;

Interact with the mock. First call the computeValue method. Then display the value of Prop2. Finally, set the value of Prop1.

n = mock.computeValue('hello');
mock.Prop1 = 13;
ans =



Obtain the interaction history for the mock.

h = matlab.mock.InteractionHistory.forMock(mock)
h = 

  1×3 heterogeneous InteractionHistory (SuccessfulMethodCall, SuccessfulPropertyAccess, SuccessfulPropertyModification) array with properties:


Interaction summary:
  computeValue([1×1 matlab.mock.classes.Mock], 'hello')
  <Mock>.Prop1 = 13

Examine the first InteractionHistory object. The method was called with the mock object and the character vector 'hello' as inputs. The method output the value 42.

ans = 

  SuccessfulMethodCall with properties:

       Name: "computeValue"
     Inputs: {[1×1 matlab.mock.classes.Mock]  'hello'}
    Outputs: {[42]}

Interaction summary:
  computeValue([1×1 matlab.mock.classes.Mock], 'hello')


You can obtain the same history of interactions using the getMockHistory method on a matlab.mock.TestCase instance. For example, if you have a matlab.mock.TestCase instance tc, and a mock object mock, the following method calls are equivalent.

h = matlab.mock.InteractionHistory.forMock(mock);
h = tc.getMockHistory(mock);

However, you do not need access to the matlab.mock.TestCase instance to use the forMock method.

Version History

Introduced in R2018a