Main Content

matlab.mock.InteractionHistory.forMock

Class:matlab.mock.InteractionHistory
Package:matlab.mock

Return history from mock object

Description

example

history = matlab.mock.InteractionHistory.forMock(mock)returns the history from a mock object.historyis an array ofmatlab.mock.InteractionHistoryobjects. Each element inhistorycorresponds 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 toHiddenmethods

  • Calls toSealedsuperclass 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 acomputeValuemethod and two properties. Assign a default value offalsetoProp2.

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

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

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

Interact with the mock. First call thecomputeValuemethod. Then display the value ofProp2. Finally, set the value ofProp1.

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') .Prop2 .Prop1 = 13

Examine the firstInteractionHistoryobject. 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 thegetMockHistorymethod on amatlab.mock.TestCase实例。For example, if you have amatlab.mock.TestCaseinstancetc, and a mock objectmock, the following method calls are equivalent.

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

However, you do not need access to thematlab.mock.TestCaseinstance to use theforMockmethod.

版本历史

Introduced in R2018a