Contenido principal

createMock

Class: matlab.mock.TestCase
Namespace: matlab.mock

Create mock object

Description

mock = createMock(testCase) creates a mock object. For more information about mock objects, see Create Mock Object.

mock = createMock(testCase,superclass) creates a mock that derives from the specified superclass.

mock = createMock(___,Name=Value) specifies options using one or more name-value arguments in addition to any of the input argument combinations in previous syntaxes. For example, mock = testCase.createMock(AddedProperties=["Prop1" "Prop2"]) creates a mock and adds properties named Prop1 and Prop2 to it.

example

[mock,behavior] = createMock(___) also returns the behavior object associated with the mock. Use the behavior object to define mock actions and verify interactions.

Input Arguments

expand all

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

expand all

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 superclass input argument

  • Methods added to the mock using the AddedMethods name-value argument

  • Properties added to the mock using the AddedProperties name-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

expand all

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

expand all

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: 0

Create 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 R2017a

expand all