han*_*aad 28 delphi unit-testing exception dunit
我想知道在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;
Run Code Online (Sandbox Code Playgroud)
Tri*_*enT 44
您可以StartExpectingException
用来围绕您的方法调用).
StartExpectingException(MyException);
MyMethod(MyParam);
StopExpectingException();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3622 次 |
最近记录: |