Main Content

matlab.mock.InteractionHistory.forMock

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

Return history from mock object

Description

example

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.

Examples

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'}, ...
    'DefaultPropertyValues',struct('Prop2',false));

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

import matlab.mock.actions.AssignOutputs;
when(withAnyInputs(behavior.computeValue),AssignOutputs(42));

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.Prop2
mock.Prop1 = 13;
ans =

  logical

   0

Obtain the interaction history for the mock.

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

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

    Name

Interaction summary:
  computeValue([1×1 matlab.mock.classes.Mock], 'hello')
  <Mock>.Prop2
  <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.

h(1)
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')

Alternatives

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