Class: matlab.mock.TestCase
Package: matlab.mock
Create mock object
[mock,behavior]
= createMock(testcase)
[mock,behavior]
= createMock(testcase,superclass)
[mock,behavior]
= createMock(___,Name,Value)
[
creates a mock object and an associated behavior object.mock,behavior]
= createMock(testcase)
[
creates a mock that derives from the mock,behavior]
= createMock(testcase,superclass)superclass class.
[
creates a mock with additional options specified by one or more
mock,behavior]
= createMock(___,Name,Value)Name,Value pair arguments. You can use this syntax with any of
the arguments from the previous syntaxes.
testcase — Instance of test casematlab.mock.TestCase objectInstance of the test case, specified as a matlab.mock.TestCase object.
superclass — Superclass for mockmeta.class objectSuperclass 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
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.
'AddedMethods' — Names of methods to add to mockNames 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'}
'AddedProperties' — Properties to add to mockNames 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'}
'AddedEvents' — Events to add to mockNames 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'}
'DefaultPropertyValues' — Default property valuesDefault 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)
'MockedMethods' — Methods to mockstring.empty | {} | string array | cellstrMethods 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
'Strict' — Indicator if mock is strictfalse (default) | trueIndicator 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
'ConstructorInputs' — Inputs to pass to superclass constructorInputs 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'}.
mock — Implementation of abstract methods and propertiesImplementation 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.
behavior — Definition of mock behaviorDefinition 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.
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: 0Construct a mock that overrides the isnan and
isinf methods of the class
double.
[mock,behavior] = testCase.createMock(?double,"MockedMethods",["isnan","isinf"],... "ConstructorInputs",{123});