2014-01-30 52 views

回答

1

在引用的問題expectAsync只是用來防止異步調用,以便在調用new Timer(...)完成之前測試不會結束。

您可以額外添加提供多長時間(最小/最大)方法必須被調用以滿足測試。 如果您的測試功能使用`expectAsync2'調用一個以上參數的方法

您引用的問題中的錯誤是您撥打expectAsyncX的電話也被延遲了。 調用expectAsyncX必須在異步功能被調用以註冊哪個方法必須被調用之前進行。

library x; 

import 'dart:async'; 
import 'package:unittest/unittest.dart'; 

class SubjectUnderTest { 
    int i = 0; 
    doSomething(x, y) { 
    i++; 
    print('$x, $y'); 
    } 
} 

void main(List<String> args) { 

    test('async test, check a function with 2 parameters',() { 
    var sut = new SubjectUnderTest(); 
    var fun = expectAsync2(sut.doSomething, count: 2, max: 2, id: 'check doSomething'); 

    new Timer(new Duration(milliseconds:200),() { 
     fun(1,2); 
     expect(sut.i, greaterThan(0)); 
    }); 

    new Timer(new Duration(milliseconds:100),() { 
     fun(3,4); 
     expect(sut.i, greaterThan(0)); 
    }); 

    }); 
} 

您可以檢查是否設置countmax3會發生什麼。

+0

謝謝,但我可以在哪裏寫'expect()'語句? – Freewind

+0

這取決於你想要的東西;-) –

+0

我延長了我的答案一點。 –

0

你可以看看文章Asynchronous tests section的文章Dart單元測試

+0

我在發佈這個問題時正在閱讀那篇文章。那篇文章沒有給我足夠的信息,我不知道我的代碼在相關問題中出了什麼問題。 – Freewind