[英]How to do a test with an anonymous with Delphi and TestInsight?
我想用匿名方法測試一個過程。 該函數使用 anonymousThread 來執行休息請求。 它通過匿名方法同步返回。
我該怎么做呢?
我正在使用 TestInsight 並擁有它。
// Calls a rest threaded request
procedure Shows.GetAgenda;
begin
var testCompleted := false;
DmShows.LoadShows(120,
procedure (ServerResult : TServerResult)
begin
try
Assert.IsTrue(ServerResult.StatusCode = 200, ServerResult.Message);
Assert.IsTrue(DmShows.TableShows.RecordCount > 0, 'No shows found for testing.');
finally
TestCompleted := true;
end;
end);
while not testCompleted do
begin
TThread.Sleep(10);
Application.ProcessMessages();
end;
end;
此示例在第一個 Assert 時在 CheckSynchronize 中拋出異常。 匿名方法已同步。
您不能從用作回調的匿名方法中運行測試框架驗證方法,因為測試框架將無法正確捕獲這些異常。
您需要引入其他變量,分配您希望在回調中驗證的適當值,然后在調用回調並設置testCompleted
標志后驗證結果。
如果您正在通過 TestInsight 運行,我不確定您是否有權訪問該Application
,因此我將Application.ProcessMessages
替換為CheckSynchronize
調用,這是處理同步代碼時實際需要的。 如果您從控制台運行 DUnitX,這將啟用正確的測試。
如果您有權訪問 TestInsight 中的Application
實例,則可以保留Application.ProcessMessages
,但在這種情況下您不能使用控制台。
procedure Shows.GetAgenda;
begin
var testCompleted := false;
var StatusCode := 0;
var StatusMessage := '';
var RecordCount := 0;
DmShows.LoadShows(120,
procedure (ServerResult : TServerResult)
begin
try
StatusCode := ServerResult.StatusCode;
StatusMessage := ServerResult.Message;
RecordCount := DmShows.TableShows.RecordCount;
finally
TestCompleted := true;
end;
end);
while not testCompleted do
begin
TThread.Sleep(10);
CheckSynchronize(1000);
end;
Assert.IsTrue(StatusCode = 200, StatusMessage);
Assert.IsTrue(RecordCount > 0, 'No shows found for testing.');
end;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.