delphi – CheckException只接受0参数方法;如何测试其他方法抛出异常?

我想知道什么是测试在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;

解决方法

你可以使用StartExpectingException包围你的方法调用)。
StartExpectingException(MyException);
MyMethod(MyParam);
StopExpectingException();

相关文章

 从网上看到《Delphi API HOOK完全说明》这篇文章,基本上都...
  从网上看到《Delphi API HOOK完全说明》这篇文章,基本上...
ffmpeg 是一套强大的开源的多媒体库 一般都是用 c/c+&#x...
32位CPU所含有的寄存器有:4个数据寄存器(EAX、EBX、ECX和ED...
1 mov dst, src dst是目的操作数,src是源操作数,指令实现的...