Main Content

when

Class: matlab.mock.PropertyGetBehavior
Package: matlab.mock

Specify mock object property access action

Syntax

when(behavior,action)

Description

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

Input Arguments

expand all

Behavior of the mock, specified as a matlab.mock.PropertyGetBehavior instance. To create an instance of matlab.mock.PropertyGetBehavior, call the matlab.mock.PropertyBehavior.get method with the behavior object.

Example: get(myMockBehavior.MyProperty)

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

Example: ReturnStoredValue

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

Examples

expand all

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

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

Set up the behavior. When the property is accessed, return the value "David".

import matlab.mock.actions.AssignOutputs
when(get(behavior.Name),AssignOutputs("David"))

Access the property.

name = mock.Name
name = 
"David"
Introduced in R2017a