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
Instance of the test case, specified as a matlab.mock.TestCase 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.
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
abstractmethods and properties of the specified interface.Methods added to the mock with the
AddedMethodsargument.Properties added to the mock with the
AddedPropertiesargument.
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
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
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});
Version History
Introduced in R2017aWhen 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.
To specify which methods to mock, use the MockedMethods
name-value argument.
You can create mocks for classes that use custom metaclasses to define custom class, property, method, and event attributes.
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.
Seleziona un sito web
Seleziona un sito web per visualizzare contenuto tradotto dove disponibile e vedere eventi e offerte locali. In base alla tua area geografica, ti consigliamo di selezionare: .
Puoi anche selezionare un sito web dal seguente elenco:
Come ottenere le migliori prestazioni del sito
Per ottenere le migliori prestazioni del sito, seleziona il sito cinese (in cinese o in inglese). I siti MathWorks per gli altri paesi non sono ottimizzati per essere visitati dalla tua area geografica.
Americhe
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)