createMock
Class: matlab.mock.TestCase
Namespace: matlab.mock
Create mock object
Syntax
Description
creates a mock object. For more information about mock objects, see Create Mock Object.mock = createMock(testCase)
creates a mock that derives from the specified superclass.mock = createMock(testCase,superclass)
specifies options using one or more name-value arguments in addition to any of the
input argument combinations in previous syntaxes. For example, mock = createMock(___,Name=Value)mock =
testCase.createMock(AddedProperties=["Prop1" "Prop2"]) creates a mock
and adds properties named Prop1 and Prop2
to it.
Input Arguments
Test case, specified as a matlab.mock.TestCase
object.
Superclass for the mock, specified as a
matlab.metadata.Class object. The mock object implements
all the abstract properties and methods of this class.
Example: ?MyClass
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.
Example: mock = testCase.createMock(AddedProperties=["Prop1"
"Prop2"])
Names of methods to add to the mock, specified as a string array or 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: AddedMethods="myMethod"
Example: AddedMethods=["methodA" "methodB"
"methodC"]
Names of properties to add to the mock, specified as a string array or cell array of character vectors. If a mock is not strict, you can set and get the values of these properties. However, if a mock is strict, by default, the mocking framework produces an assertion failure if you set or get a property value.
Example: AddedProperties="MyProperty"
Example: AddedProperties=["Prop1"
"Prop2"]
Names of events to add to the mock, specified as a string array or cell array of character vectors. To add events to the mock, the mock object must derive from a handle class.
Example: AddedEvents="MyEvent"
Example: AddedEvents=["Event1"
"Event2"]
Default property values, specified as a scalar structure. Use this
name-value argument to specify default values for properties implemented
by the class of the mock object. These properties include abstract
superclass properties and properties added with the
AddedProperties name-value argument. Specify
each structure field name as the name of a mock class property, and
specify the corresponding field value as the default value for that
property.
Example: DefaultPropertyValues=struct("PropA",123,"PropB",true)
Names of the mocked methods, specified as a string array or cell array
of character vectors. By default, all methods are mocked. To specify
that no methods are mocked, use an empty value specified as
string.empty or {}.
MockedMethods can include any subset of added
methods, abstract superclass methods, and concrete superclass methods
that can be overridden (have a Sealed attribute value
of false). In general, you include only the 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 testing performance when
superclasses define many methods.
Example: MockedMethods=string.empty
Example: MockedMethods=["foo" "bar"]
Option to create a strict mock, specified as a numeric or logical
0 (false) or
1 (true). By default, if the
behavior is undefined, a mock method returns an empty array without an
assertion failure. However, if you specify Strict
as true, the mocking framework produces an assertion
failure for undefined behavior for:
All abstract methods and properties of the interface specified by the
superclassinput argumentMethods added to the mock using the
AddedMethodsname-value argumentProperties added to the mock using the
AddedPropertiesname-value argument
Example: Strict=true
Inputs to pass to the superclass constructor,
specified as a cell array.
For example, if you create a mock where you specify
superclass as ?MException,
then you might specify ConstructorInputs as
{'My:ID','My message'}.
Example: ConstructorInputs={'My:ID','My
message'}
Output Arguments
Implementation of the abstract methods and properties of the interface
specified by the superclass input argument, returned as
a mock object. If you create a mock without specifying a superclass, the
mock does not have an explicit interface.
Note
Saving or loading mock objects is not supported.
Definition of the mock behavior, returned as a behavior object. For more information about behavior objects, see Specify Mock Object Behavior.
Note
Saving or loading behavior objects is not supported.
Examples
Create a test case for interactive testing.
testCase = matlab.mock.TestCase.forInteractiveUse;
Create a strict mock.
mock = testCase.createMock(AddedMethods="foo",Strict=true);Create a mock with specific methods.
mock = testCase.createMock(AddedMethods=["one" "two" "three"]);
Create a mock with specific events.
mock = testCase.createMock(?handle,AddedEvents=["EventA" "EventB"]);
Create a mock with constructor inputs.
mock = testCase.createMock(?MException, ... ConstructorInputs={'My:ID','My message'});
Create a mock with two properties named Prop1 and
Prop2. Specify the default value of the
Prop2 property as false.
mock = testCase.createMock( ... AddedProperties=["Prop1" "Prop2"], ... DefaultPropertyValues=struct("Prop2",false))
mock =
Mock with properties:
Prop1: []
Prop2: 0Create a mock that overrides the isnan and
isinf methods of the double
class.
mock = testCase.createMock(?double, ... MockedMethods=["isnan" "isinf"], ... ConstructorInputs={123});
Since R2026a
Create a mock for a class that has a property with both the
Abstract and WeakHandle attributes.
For more information about WeakHandle properties, see Weak References.
In a file named Example.m in your current folder,
create the Example class. Define an abstract property with
the WeakHandle attribute.
classdef Example properties (Abstract, WeakHandle) Prop matlab.lang.HandlePlaceholder end end
Create a test case for interactive testing.
testCase = matlab.mock.TestCase.forInteractiveUse;
Create a mock for the Example class. For the class of the
mock object, specify the default value of the property as an empty matlab.lang.HandlePlaceholder object.
mock = testCase.createMock(?Example, ... DefaultPropertyValues= ... struct("Prop",matlab.lang.HandlePlaceholder.empty))
mock =
ExampleMock with properties:
Prop: [0×0 matlab.lang.HandlePlaceholder]Version History
Introduced in R2017aYou can create mocks for classes that have properties with both the
Abstract and WeakHandle attributes. In
previous releases, you can create a mock for a class with
WeakHandle properties only if those properties are
concrete.
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.
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.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
Cómo obtener el mejor rendimiento
Seleccione China (en idioma chino o inglés) para obtener el mejor rendimiento. Los sitios web de otros países no están optimizados para ser accedidos desde su ubicación geográfica.
América
- 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)