createMock
Class: matlab.mock.TestCase
Namespace: matlab.mock
Create mock object
Syntax
[mock,behavior]
= createMock(testcase)
[mock,behavior]
= createMock(testcase,superclass)
[mock,behavior]
= createMock(___,Name,Value)
Description
[
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.
Input Arguments
testcase
— Instance of test case
matlab.mock.TestCase
object
Instance of the test case, specified as a matlab.mock.TestCase
object.
superclass
— Superclass for mock
scalar matlab.metadata.Class
object
Superclass for mock, specified as a scalar
matlab.metadata.Class
object. The mock object implements
all the abstract properties and methods of this class.
Example: ?MyIterfaceClass
Example: ?MException
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
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 mock
string array | cell array of character vectors
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'}
AddedProperties
— Properties to add to mock
string array | cell array of character vectors
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'}
AddedEvents
— Events to add to mock
string array | cell array of character vectors
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'}
DefaultPropertyValues
— Default property values
scalar struct
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)
MockedMethods
— Methods to mock
mock all possible methods (default) | string.empty
| {}
| string array | cellstr
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
Strict
— Indicator if mock is strict
false
(default) | true
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
ConstructorInputs
— Inputs to pass to superclass
constructor
cell array of values
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
mock
— Implementation of abstract methods and properties
mock object
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.
behavior
— Definition of mock behavior
behavior object
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
Construct Mocks
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});
Version History
Introduced in R2017aR2020a: Add events to mock objects
When creating a mock object that derives from a handle class, you can add events
to the object in addition to properties and methods. To specify the events to mock,
use the AddedEvents
name-value argument.
R2019a: Specify which methods to mock
To specify which methods to mock, use the MockedMethods
name-value argument.
R2019a: Create mocks for classes that use custom metaclasses
You can create mocks for classes that use custom metaclasses to define custom class, property, method, and event attributes.
R2019a: Create mocks for classes that use property validation
You can create mocks for classes that use property validation. For information on property validation, see Validate Property Values.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)