29

dunit で例外をテストするためのベスト プラクティスは何だろうと思っています。私は Delphi のメソッド ポインタにあまり詳しくありません。引数なしで呼び出すことができるように、引数をメソッドポインタにバインドする可能性はありますか? 現時点では、この「バインド」を手動で行う追加のメソッドを常に作成しています。SUT に多くのスロー メソッドがある場合、これは煩わしくなります。

// What i did before i knew abput CheckExcepion
procedure MyTest.MyMethod_BadInput_Throws;
var
    res: Boolean;
begin
    res := false;
    try
        sut.MyMethod('this is bad');
    except
        on e : MyExpectedException do:
            res := true;
    end;
    CheckTrue(res);
end;

// What i do now
procedure MyTest.MyMethodWithBadInput;
begin
    sut.MyMethod('this is bad');
end;

procedure MyTest.MyMethod_BadInput_Throws;
begin
    CheckException(MyMethodWithBadInput, MyExpectedException);
end;

// this would be nice
procedure MyTest.MyMethod_BadInput_Throws;
begin
    CheckException(
        BindArguments(sut.MyMethod, 'this is bad'),  // <-- how to do this
        MyExpectedException);
end;
4

5 に答える 5

45

StartExpectingExceptionメソッド呼び出しを囲むために使用できます)。

StartExpectingException(MyException);
MyMethod(MyParam);
StopExpectingException();
于 2011-01-06T10:12:14.833 に答える
3

前述のように、これは匿名メソッドにとって最適な場所です。

これが私のやり方です。Alex Ciobanu からこれを「借りました」:

procedure TestTMyClass.CheckException(aExceptionType: TClassOfException; aCode: TTestCode; const aMessage: String);
var
  WasException: Boolean;
begin
  WasException := False;
  try
    aCode;
  except
    on E: Exception do
    begin
      if E is aExceptionType then
      begin
        WasException := True;
      end;
    end;
  end;
  Check(WasException, aMessage);
end;

次に、次のように呼び出します。

CheckException(ETestingException, 
             procedure begin FMyClass.RaiseTestingException end,      
             'The ETestingException exception didn''t get raised.  That is impossible!');
于 2011-04-10T23:58:01.530 に答える
3

DUnit がそれをサポートしているかどうかはまだわかりませんが、これは Delphi 2010 で導入された匿名メソッドの完全な使用例です。DUnit がサポートしていない場合は、ソースを自分で簡単に変更できます。

于 2011-01-06T09:25:56.793 に答える
2

StartExpectingException()複数の例外ケースをテストする場合に使用するのは最善の方法ではありません。テスト手順で考えられるすべてのケースをテストするために、例外とともに次のアルゴリズムを使用します。

uses
  Dialogs;
procedure MyTest.MyMethod_Test;
begin
  // Test for Exceptions
  try
    MyMethod(MyParam1CreatingException1);
    ShowMessage('Error! There should have been exception: Exxx here!');
    Check(false);
  except on E: Exception do Check(E is  ExceptionType1); end; // This exception is OK
  try
    MyMethod(MyParam2CreatingException2);
    ShowMessage('Error! There should have been exception: Exxx here!');
    Check(false);
  except on E: Exception do Check(E is  ExceptionType2); end; // This exception is OK
  // ... test other exceptions ...

  // Test other parameters
  CheckEquals('result1', MyMethod(MyParam1));
  CheckEquals('result2', MyMethod(MyParam2));
  // ... other tests ...
end;

ShowMessage('Error! There should be exception: Exxx here!'); 提供されたメソッドの代わりに使用する理由Check(false, 'There should have been an EListError.');は、私の場合 (Delphi6) が機能しないためです。Check(boolean, 'Message')チェックがtry...exceptブロック内にある場合はメッセージが表示されません (理由がわからない)。

于 2013-08-05T04:17:24.970 に答える
0

これは、DUnit のサブクラス化された Nick Hodges の回答の実用的で改善されたバージョンですTestFramework.TTestCase

uses
  TestFramework, System.SysUtils;
type
  TTestCode = reference to procedure;

  TTestCasePlus = class(TestFramework.TTestCase)
    procedure CheckException(
      ExceptionType: TClass; Code: TTestCode; const Message: String = '');
  end;

implementation

procedure TTestCasePlus.CheckException(
  ExceptionType: TClass; Code: TTestCode; const Message: String = '');
{ Check whether some code raises a specific exception type.

Adapted from http://stackoverflow.com/a/5615560/797744

Example:

  Self.CheckException(EConvertError,
                      procedure begin UnformatTimestamp('invalidstr') end);

@param ExceptionType: The exception class which we check if it was raised.
@param Code: Code in the form of an anonymous method that should raise the
  exception.
@param Message: Output message on check failure. }
var
  WasRaised: Boolean;
begin
  WasRaised := False;
  try
    Code;
  except
    on E: Exception do
      if E is ExceptionType then
        WasRaised := True;
  end;
  Check(WasRaised, Message);
end;

例外が発生したかどうかを確認するこの方法の優れたボーナスStart/StopExpectingException()は、デバッグ ビルドでテストランナーを実行できることです。たとえそれが処理されたとしても、例外が発生するたびに。

于 2016-03-17T11:38:22.203 に答える