MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
testCase.throwExceptionWhen(set(behaviorObj.Name))
mock.Name = "Sue";

Error using matlab.mock.internal.MockContext/createMockObject/mockPropertySetCallback (line 368)
The following property set was specified to throw an exception:
<Mock>.Name = "Sue"

Allow the mock to store the value when the property is set to "David".

testCase.storeValueWhen(setToValue(behaviorObj.Name,"David"));
mock.Name = "David"

mock =

Mock with properties:

Name: "David"
ID: []

Define Repeating and Subsequent Behavior


The matlab.mock.TestCase methods are convenient for defining behavior. However,
there is more functionality when you use a class in the matlab.mock.actions package
instead. Using these classes, you can define behavior that repeats the same action
multiple times and specify subsequent actions. To define repeating or subsequent
behavior, pass an instance of a class in the matlab.mock.actions package to the when
method of the behavior class.

Assign the value of 1138 to the ID property and then throw an exception for property
access.

import matlab.mock.actions.AssignOutputs
import matlab.mock.actions.ThrowException
when(get(behaviorObj.ID),then(AssignOutputs(1138),ThrowException))
id = mock.ID
id = mock.ID

id =

1138

Error using matlab.mock.internal.MockContext/createMockObject/mockPropertyGetCallback (line 346)
The following property access was specified to throw an exception:
<Mock>.ID

33 Unit Testing

Free download pdf