Main Content

setToValue

Class: matlab.mock.PropertyBehavior
Package: matlab.mock

Construct object to define behavior when mocked property is set to specific value

Syntax

setBehavior = setToValue(behavior,value)

Description

setBehavior = setToValue(behavior,value) constructs a PropertySetBehavior object to define behavior when mocked property is set to a specific value. Typically you use the setToValue method to construct the PropertySetBehavior implicitly when you define mock behavior.

Input Arguments

expand all

Behavior of the mock, specified as a matlab.mock.PropertyBehavior instance. To create an instance of matlab.mock.PropertyBehavior, access a property of the behavior object.

Example: myMockBehavior.MyProperty

Property values that the mock property must be set to, specified as a scalar, vector, matrix, multidimensional array, constraint, or behavior object. Values can be any data type, and relate to the property specified by behavior.

Example: "hello"

Example: 42

Example: [1 2 3]

Example: matlab.unittest.constraints.IsLessThan(10)

Examples

expand all

Create a mock for a person class with a Name property.

testCase = matlab.mock.TestCase.forInteractiveUse;
[mock,behavior] = testCase.createMock('AddedProperties',"Name");

Create a PropertySetBehavior object and set up the behavior. The mock throws an exception when you set the value of the Name property to "David".

setBehavior = setToValue(behavior.Name,"David");
testCase.throwExceptionWhen(setBehavior)

Alternatively, you can create the PropertySetBehavior object implicitly with the behavior definition.

testCase.throwExceptionWhen(behavior.Name.setToValue("David"))

Set the value of the Name property.

mock.Name = "Andy";
mock.Name = "David";
Error using matlab.mock.internal.MockContext/createMockObject/mockPropertySetCallback (line 420)
The following property set was specified to throw an exception:
	<Mock>.Name = "David"
Introduced in R2017a