when
Class: matlab.mock.PropertySetBehavior
Namespace: matlab.mock
Specify mock object property set action
Syntax
when(behavior,action)
Description
when(
specifies
the action that a mock object property takes when it is set.behavior
,action
)
Input Arguments
behavior
— Behavior of mock
matlab.mock.PropertySetBehavior
instance
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)
action
— Defined action
instance of matlab.mock.actions.StoreValue
| instance of matlab.mock.actions.DoNothing
| instance of matlab.mock.actions.ThrowException
Defined action, specified as an instance of matlab.mock.actions.StoreValue
,
matlab.mock.actions.DoNothing
, or
matlab.mock.actions.ThrowException
.
Example: StoreValue
Example: ThrowException(MException('Account:deposit:Negative','Deposit
amount must be positive.'))
Examples
Specify Mock Property Set Behavior
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"
Version History
Introduced in R2017a
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.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)