when

Class: matlab.mock.PropertySetBehavior
Package: matlab.mock

Specify mock object property set action

Syntax

when(behavior,action)

Description

when(behavior,action) specifies the action that a mock object property takes when it is set.

Input Arguments

expand all

Behavior of the mock, specified as a matlab.mock.PropertySetBehavior instance. To create an instance of matlab.mock.PropertySetBehavior, call the matlab.mock.PropertyBehavior.set or matlab.mock.PropertyBehavior.setToValue method with the behavior object.

Example: set(myMockBehavior.MyProperty)

Example: setToValue(myMockBehavior.MyProperty,13)

Defined action, specified as an instance of matlab.mock.actions.StoreValue or matlab.mock.actions.ThrowException.

Example: StoreValue

Example: ThrowException(MException('Account:deposit:Negative','Deposit amount must be positive.'))

Examples

expand all

Create a strict mock for a person class. The mock has one property, Name.

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

Set up the behavior. When the property is set, store the value, and when the property is accessed, return the value. Without defining this behavior, strict mocks throw an exception when setting or accessing the property.

import matlab.mock.actions.StoreValue
import matlab.mock.actions.ReturnStoredValue
when(set(behavior.Name),StoreValue)
when(get(behavior.Name),ReturnStoredValue)

Set the property. The property access is implicit because we are displaying the result.

mock.Name = "David"
mock = 
  Mock with properties:

    Name: "David"

Introduced in R2017a