when
クラス: matlab.mock.MethodCallBehavior
名前空間: matlab.mock
モック オブジェクト メソッドの動作を指定
構文
when(behavior,action)
入力引数
behavior
— モックの動作
matlab.mock.MethodCallBehavior
インスタンス
モックの動作。matlab.mock.MethodCallBehavior
インスタンスとして指定します。matlab.mock.MethodCallBehavior
のインスタンスを作成するには、behavior オブジェクトのメソッドを呼び出します。
例: withExactInputs(myMockBehavior.myMockedMethod)
action
— 定義済みのアクション
matlab.mock.actions.AssignOutputs
のインスタンス | matlab.mock.actions.Invoke
のインスタンス | matlab.mock.actions.DoNothing
のインスタンス | matlab.mock.actions.ThrowException
のインスタンス
定義済みのアクション。matlab.mock.actions.AssignOutputs
、matlab.mock.actions.Invoke
、matlab.mock.actions.DoNothing
、または matlab.mock.actions.ThrowException
のインスタンスとして指定します。
例: AssignOutputs(7,13,42)
例: ThrowException(MException('Account:deposit:Negative','Deposit amount must be positive.'))
例
モック メソッドの動作を指定
三角形クラスのモックを作成します。このモックには 1 つのメソッド (sideLengths
) があります。
import matlab.mock.actions.AssignOutputs; testCase = matlab.mock.TestCase.forInteractiveUse; [mock,behavior] = testCase.createMock('AddedMethods',"sideLengths");
動作を設定します。sideLengths
メソッドの入力にかかわらず、このモックは 2、3、4 の各値を返します。
when(withAnyInputs(behavior.sideLengths),AssignOutputs(2,3,4))
モック オブジェクトの sideLengths
メソッドを呼び出します。
[a,b,c] = mock.sideLengths
a = 2
b = 3
c = 4
複数の入力と 2 つだけの出力を使用して、sideLengths
メソッドを再度呼び出します。
[a,b] = mock.sideLengths(13,"inputText")
a = 2
b = 3
バージョン履歴
R2017a で導入
MATLAB コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは 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)