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(
returns a n
,behavior
)MethodCallBehavior
object to define behavior when mock object
method is called with a defined number of output arguments.
Input Arguments
n
— Number of outputs
integer
Number of outputs from mock object method, specified as an integer.
Example: 3
behavior
— Behavior of mock
matlab.mock.MethodCallBehavior
instance
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
Specify Method Call with Specific Number of Outputs
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
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 (한국어)