Main Content

assignOutputsWhen

Class: matlab.mock.TestCase
Namespace: matlab.mock

Define return values for method call or property access

Syntax

assignOutputsWhen(testcase,behavior,A1,...,An)

Description

assignOutputsWhen(testcase,behavior,A1,...,An) defines values to return for a method called or property accessed.

Input Arguments

expand all

Instance of the test case, specified as a matlab.mock.TestCase object.

Behavior of mock, specified as a matlab.mock.MethodCallBehavior or a matlab.mock.PropertyGetBehavior instance. To create an instance of matlab.mock.MethodCallBehavior, call a method of the behavior object. To create an instance of matlab.mock.PropertyGetBehavior, call the get method on a property of the behavior object.

Example: get(behavior.MyMockedProperty)

Example: withExactInputs(behavior.myMockedMethod)

Defined return values, specified as scalars, vectors, matrices, or multidimensional arrays. Return values can be any data type, and relate to the property or method specified by behavior.

Example: "mySpecifiedValue"

Example: 7,13,42

Example: [1 2 3;4 5 6]

Examples

expand all

Assign outputs.

testCase = matlab.mock.TestCase.forInteractiveUse;
[mock, behavior] = testCase.createMock('AddedProperties',"PropertyFoo", ...
    'AddedMethods',"methodBar");
testCase.assignOutputsWhen(get(behavior.PropertyFoo), 'abc');
testCase.assignOutputsWhen(withExactInputs(behavior.methodBar), 1, 2, 3);

% Carry out actions
mock.PropertyFoo
[out1,out2,out3] = mock.methodBar

Alternatives

Using the assignOutputsWhen method is functionally equivalent to using the matlab.mock.actions.AssignOutputs action with the when method of the MethodCallBehavior or PropertyGetBehavior class. For example, the following code blocks are functionally equivalent.

% Using the assignOutputsWhen method
testCase.assignOutputsWhen(get(behavior.PropertyFoo),'abc');
testCase.assignOutputsWhen(withExactInputs(behavior.methodBar),1,2,3);

% Using the AssignOutputs action with the when function
import matlab.mock.actions.AssignOutputs;
when(get(behavior.PropertyFoo),AssignOutputs('abc'));
when(withExactInputs(behavior.methodBar),AssignOutputs(1,2,3));
However, there is more functionality when you use the AssignOutputs action. For instance, you can specify different subsequent behavior for the same mocked object interaction.

Version History

Introduced in R2017a