Main Content

withNargout

Class: matlab.mock.MethodCallBehavior
Namespace: matlab.mock

Specify mock object method call with defined number of output arguments

Syntax

callBehavior = withNargout(n,behavior)

Description

callBehavior = withNargout(n,behavior) returns a MethodCallBehavior object to define behavior when mock object method is called with a defined number of output arguments.

Input Arguments

expand all

Number of outputs from mock object method, specified as an integer.

Example: 3

Behavior of the mock, specified as a matlab.mock.MethodCallBehavior instance. To create an instance of matlab.mock.MethodCallBehavior, call a method of the behavior object.

Example: myMockBehavior.myMockedMethod

Examples

expand all

Create a mock for a triangle class.

testCase = matlab.mock.TestCase.forInteractiveUse;
[mockQuad,behavior] = testCase.createMock('AddedMethods',"sideLengths");

Set up behavior. If the sideLengths method is called with only the object as input and with one output, then return an array of three values. If it is called with only the object as input and with three outputs, then return the three values. Otherwise, return the default value of an empty array.

import matlab.mock.actions.AssignOutputs
when(withNargout(1,withExactInputs(behavior.sideLengths)), ...
    AssignOutputs([1 2 3]))
when(withNargout(3,withExactInputs(behavior.sideLengths)), ...
    AssignOutputs(1,2,3))

Call the sideLengths method with only the object as an input and one output.

len = mockQuad.sideLengths
len = 1×3

     1     2     3

Verify that the sideLengths method was called at least once with one output argument.

import matlab.mock.constraints.WasCalled
testCase.verifyThat(withNargout(1, ...
    withExactInputs(behavior.sideLengths)),WasCalled)
Verification passed.

Verify that the sideLengths method was not called with three output arguments.

testCase.verifyThat(withNargout(3, ...
    withExactInputs(behavior.sideLengths)),~WasCalled)
Verification passed.

Version History

Introduced in R2017a