Main Content

throwExceptionWhen

クラス: matlab.mock.TestCase
名前空間: matlab.mock

メソッドの呼び出しやプロパティの相互作用に対して例外をスロー

構文

throwExceptionWhen(testcase,behavior)
throwExceptionWhen(testcase,behavior,exception)

説明

throwExceptionWhen(testcase,behavior) は、メソッドが呼び出された場合、またはプロパティがアクセスまたは設定された場合にモックが例外をスローすることを指定します。

throwExceptionWhen(testcase,behavior,exception) は、モックがスローする例外を指定します。

入力引数

すべて展開する

テスト ケースのインスタンス。matlab.mock.TestCase オブジェクトとして指定します。

モックの動作。matlab.mock.MethodCallBehaviormatlab.mock.PropertyGetBehavior または matlab.mock.PropertySetBehavior インスタンスとして指定します。matlab.mock.MethodCallBehavior のインスタンスを作成するには、behavior オブジェクトのメソッドを呼び出します。matlab.mock.PropertyGetBehavior のインスタンスを作成するには、behavior オブジェクトのプロパティで get メソッドを呼び出します。matlab.mock.PropertySetBehavior のインスタンスを作成するには、behavior オブジェクトのプロパティで set メソッドを呼び出します。

例: withExactInputs(behavior.myMockedMethod)

例: get(behavior.MyMockedProperty)

例: set(behavior.MyMockedProperty)

メソッドの呼び出しやプロパティの操作が実行された場合にフレームワークがスローする例外。スカラー MException オブジェクトとして指定します。

例: MException('MyProduct:myID','My exception message.')

すべて展開する

メソッドが呼び出された場合、またはプロパティがアクセスまたは設定された場合に例外をスローします。

testCase = matlab.mock.TestCase.forInteractiveUse;
[mock,behavior] = testCase.createMock('AddedProperties',"PropertyFoo", ...
    'AddedMethods',"methodBar");
testCase.throwExceptionWhen(get(behavior.PropertyFoo));
testCase.throwExceptionWhen(set(behavior.PropertyFoo), ...
    MException('PropertyFoo:set', 'Do not change PropertyFoo'));
testCase.throwExceptionWhen(withAnyInputs(behavior.methodBar));

% Carry out actions
mock.PropertyFoo
mock.PropertyFoo = 123;
mock.methodBar;

代替方法

throwExceptionWhen メソッドの使用は、matlab.mock.actions.ThrowException アクションを MethodCallBehavior クラス、PropertyGetBehavior クラスまたは PropertySetBehavior クラスの when メソッドとあわせて使用するのと機能的には同じです。たとえば、次のコード ブロックは機能的に等価です。

% Using the throwExceptionWhen method
testCase.throwExceptionWhen(behavior.deposit(IsLessThan(0)), ...
    MException('Account:deposit:Negative', ...
    'Deposit amount must be positive.'));

% Using the ThrowException action with the when function
import matlab.mock.actions.ThrowException
when(behavior.deposit(IsLessThan(0)),ThrowException( ...
    MException('Account:deposit:Negative', ...
    'Deposit amount must be positive.')))
ただし、ThrowException アクションを使用すると、さらに多くの機能を使用できます。たとえば、同じモック オブジェクトの相互作用に対して異なる後続の動作を指定することができます。

バージョン履歴

R2017a で導入