when
Class: matlab.mock.MethodCallBehavior
Package: matlab.mock
Specify mock object method behavior
Syntax
when(behavior,action)
Description
when(
specifies
the action that a mock object method takes when it is called with
the inputs defined by behavior
,action
)behavior
.
Input Arguments
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: withExactInputs(myMockBehavior.myMockedMethod)
action
— Defined action
instance of matlab.mock.actions.AssignOutputs
| instance of matlab.mock.actions.Invoke
| instance of matlab.mock.actions.DoNothing
| instance of matlab.mock.actions.ThrowException
Defined action, specified as an instance of
matlab.mock.actions.AssignOutputs
,
matlab.mock.actions.Invoke
,
matlab.mock.actions.DoNothing
, or
matlab.mock.actions.ThrowException
.
Example: AssignOutputs(7,13,42)
Example: ThrowException(MException('Account:deposit:Negative','Deposit
amount must be positive.'))
Examples
Specify Mock Method Behavior
Create a mock for a triangle class. The mock has one method, sideLengths
.
import matlab.mock.actions.AssignOutputs; testCase = matlab.mock.TestCase.forInteractiveUse; [mock,behavior] = testCase.createMock('AddedMethods',"sideLengths");
Set up behavior. Regardless of the inputs to the sideLengths
method, the mock returns the values 2, 3, and 4.
when(withAnyInputs(behavior.sideLengths),AssignOutputs(2,3,4))
Call the sideLengths
method of the mock object.
[a,b,c] = mock.sideLengths
a = 2
b = 3
c = 4
Call the sideLengths
method again using different inputs and only two outputs.
[a,b] = mock.sideLengths(13,"inputText")
a = 2
b = 3
Version History
Introduced in R2017a
Abrir ejemplo
Tiene una versión modificada de este ejemplo. ¿Desea abrir este ejemplo con sus modificaciones?
Comando de MATLAB
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.
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)