createMock

Class: matlab.mock.TestCase
Package: matlab.mock

Create mock object

Syntax

[mock,behavior] = createMock(testcase)
[mock,behavior] = createMock(testcase,superclass)
[mock,behavior] = createMock(___,Name,Value)

Description

[mock,behavior] = createMock(testcase) creates a mock object and an associated behavior object.

[mock,behavior] = createMock(testcase,superclass) creates a mock that derives from the superclass class.

[mock,behavior] = createMock(___,Name,Value) creates a mock with additional options specified by one or more Name,Value pair arguments. You can use this syntax with any of the arguments from the previous syntaxes.

Input Arguments

expand all

Instance of the test case, specified as a matlab.mock.TestCase object.

Superclass for mock, specified as a scalar meta.class object. The mock object implements all the abstract properties and methods of this class.

Example: ?MyIterfaceClass

Example: ?MException

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

For example, [mock,behavior] = testCase.createMock('AddedProperties',{'Prop1','Prop2'}) creates a mock and adds the Prop1 and Prop2 properties to it.

Names of methods to add to the mock, specified as a string array or a cell array of character vectors. Unless the mock is strict or the mock behavior has been defined, calling these methods on the mock returns an empty array.

Example: ["methodA","methodB","methodC"]

Example: {'method1','method2'}

Names of properties to add to the mock, specified as a string array or a cell array of character vectors. If a mock is not strict, you can set and get their values. However, if a mock is strict, by default MATLAB® produces an assertion failure if you set or get a property value.

Example: "MyProperty"

Example: {'Prop1','Prop2'}

Names of events to add to the mock, specified as a string array or a cell array of character vectors. To add events to the mock, the mock object must derive from a handle class.

Example: "MyEvent"

Example: {'Event1','Event2'}

Default property values, specified as a scalar struct. Use this name-value pair argument to specify default values for properties implemented by the mock object class. These properties include Abstract superclass properties and properties added with the 'AddedProperties' name-value pair argument. Each field refers to the name of a property implemented on the mock class, and the corresponding value represents the default value for that property.

Example: struct('PropA',123,'PropB',true)

Methods to mock, specified using the method names in a string array or cell array of character vectors. To specify that no methods are mocked, use an empty value specified as string.empty, or {}. By default, all methods are mocked.

MockedMethods can include any subset of added methods, abstract superclass methods, and concrete superclass methods that can be overridden (Sealed attribute value of false). In general, you include only those methods that you want to stub or spy on.

Specifying MockedMethods enables tests to mock only those methods that are important to the test case. Limiting the methods that are mocked can improve test performance when superclasses define many methods.

Example: ["foo", "bar"]

Data Types: char | string | cell

Indicator if mock is strict, specified as false or true. By default, a mock method returns an empty array if the behavior is undefined. If you set Strict to true, the framework produces an assertion failure for undefined behavior for

  • All abstract methods and properties of the specified interface.

  • Methods added to the mock with the AddedMethods argument.

  • Properties added to the mock with the AddedProperties argument.

Data Types: logical

Inputs to pass to the superclass constructor, specified as a cell array of values.

Example: If you construct a mock where you define superclass to be ?MException, 'ConstructorInputs' could be {'My:ID','My message'}.

Output Arguments

expand all

Implementation of the abstract methods and properties of the interface specified by the superclass input, returned as a mock object. If a mock is constructed without defining a superclass, it does not have an explicit interface.

Note: You cannot save and load mock objects.

Definition of the mock behavior, returned as a behavior object. Use behavior to define mock actions and verify interactions.

Note: You cannot save and load behavior objects.

Examples

expand all

Create a test case for interactive testing.

testCase = matlab.mock.TestCase.forInteractiveUse;

Construct a strict mock.

[mock,behavior] = testCase.createMock('AddedMethods',"foo",'Strict',true);

Construct a mock with specific methods.

[mock,behavior] = testCase.createMock('AddedMethods',...
    {'one','two','three'});

Construct a mock with specific events.

[mock,behavior] = testCase.createMock(?handle,'AddedEvents',...
    {'EventA','EventB'});

Construct a mock with constructor inputs.

[mock,behavior] = testCase.createMock(?MException,'ConstructorInputs',...
    {'My:ID','My message'});

Construct a mock with two properties. Prop2 has a default value of false.

mock = testCase.createMock('AddedProperties',{'Prop1','Prop2'},...
    'DefaultPropertyValues',struct('Prop2',false))
mock = 

  Mock with properties:

    Prop1: []
    Prop2: 0

Construct a mock that overrides the isnan and isinf methods of the class double.

[mock,behavior] = testCase.createMock(?double,"MockedMethods",["isnan","isinf"],...
    "ConstructorInputs",{123});

Introduced in R2017a