2014-09-29 80 views
2

所有,飛鏢單元測試 - 始終通過

下面是檢查

main() { 
    test("Resource Manager Image Load",() { 
    ResourceManager rm = new ResourceManager(); 
    int WRONG_SIZE = 1000000; 

    rm.loadImageManifest("data/rm/test_images.yaml").then((_){ 
     print("Length="+ rm.images.length.toString()); // PRINTS '6' - WHICH IS CORRECT 
     expect(rm.images, hasLength(WRONG_SIZE)); 
    }); 
    }); 
} 

我從瀏覽器中運行這個集合的大小的單元測試(客戶端飛鏢庫在使用中),並且總是通過,不管WRONG_SIZE的值如何。

幫助讚賞。

回答

2

在這種簡單的情況下,你可以只返回未來。單元測試框架可以識別它並等待未來完成。這也適用於setUp/tearDown

main() { 
    test("Resource Manager Image Load",() { 
    ResourceManager rm = new ResourceManager(); 
    int WRONG_SIZE = 1000000; 

    return rm.loadImageManifest("data/rm/test_images.yaml").then((_) { 
    //^^^^ 
     print("Length="+ rm.images.length.toString()); // PRINTS '6' - WHICH IS CORRECT 
     expect(rm.images, hasLength(WRONG_SIZE)); 
    }); 
    }); 
} 
+0

沒有意識到;好多了! – 2014-09-29 06:26:12

+0

我完成了這個 - 除了我沒有使用'return rm ....'。現在它的工作。謝謝。我確實發現這種'異步無處不在'的方法,Dart需要一些適應... – DataMania 2014-09-29 07:21:52

+0

是的,那很好。你做了什麼呢? – 2014-09-29 07:22:58

1

問題是您的代碼返回Future,並且您的測試在未來的代碼完成之前完成,因此不會導致它失敗。

查看Dart網站上的Asynchronous Tests部分。像expectAsync這樣的方法允許將來傳遞給測試框架,以便它可以等待它們完成並正確處理結果。

下面是一個例子(注意,現在expect呼叫是傳遞給expectAsync在函數內部)

test('callback is executed once',() { 
    // wrap the callback of an asynchronous call with [expectAsync] if 
    // the callback takes 0 arguments... 
    var timer = Timer.run(expectAsync(() { 
    int x = 2 + 3; 
    expect(x, equals(5)); 
    })); 
});