我想知道在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) o在Delphi中,SysUtils是一个ScanBlanks程序:
procedure ScanBlanks(const S: string; var Pos: Integer);
var
I: Integer;
begin
I := Pos;
while (I <= Length(S)) and (S[I] = ' ') do Inc(I);
Pos := I;
end;
Run Code Online (Sandbox Code Playgroud)
我想知道为什么程序使用I变量.我们不能Pos直接使用use var吗?
procedure ScanBlanks(const S: string; var Pos: Integer);
begin
while (Pos <= Length(S)) and (S[Pos] = ' ') do Inc(Pos);
end;
Run Code Online (Sandbox Code Playgroud)
是因为一些速度/内存惩罚?更有经验的人可以解释一下原因/差异吗?
我需要一个转换TDateTime到String与微秒精度。在毫秒精度的情况下,可以使用格式设置:
DateTimeToString(Result, 'd.m.yyyy hh:nn:ss.zzz', dateTime);
Run Code Online (Sandbox Code Playgroud)
但是我还需要三位数(微秒)。
可以采用小数部分并将其除以1/86400/1000000,但我正在寻找更有效的方法进行转换。