2013-03-27 64 views
1

爲什麼不這項工作?與飛鏢異步測試奇怪的行爲

var validUri = 'postgresql://user:[email protected]:5432/testdb'; 

test('Query on closed connection.',() { 
    connect(validUri).then((conn) { 
    conn.close(); 
    conn.query("select 'blah'").toList() 
     .then((_) => throw new Exception('Should not be reached.')) 
     .catchError(expectAsync1((err) {})); 
    }); 
}); 

test('Execute on closed connection.',() { 
    connect(validUri).then((conn) { 
    conn.close(); 
    conn.execute("select 'blah'") 
     .then((_) => throw new Exception('Should not be reached.')) 
     .catchError(expectAsync1((err) {}); 
    }); 
}); 

但是,如果一個人改變過去catchError回調分配:

(...) 

test('Execute on closed connection.',() { 
    var cb = expectAsync1((e) {}); 
    connect(validUri).then((conn) { 
    conn.close(); 
    conn.execute("select 'blah'") 
     .then((_) => throw new Exception('Should not be reached.')) 
     .catchError(cb); 
    }); 
}); 

它的工作原理!

我喜歡閱讀提供了很好的解釋,也許在達特異步測試一個教訓或兩個:-)

編輯: 的問題是,第一個例子做的工作 - 它傳遞報道!它不應該有。我假設expectAsyncX()必須在以後的測試中被回調。

這與測試框架的問題嗎?這類問題不應該被默默地忽略。

回答

4

任何異步調用應expectAsyncX()包裹告訴測試等待其呼叫。 在你的第一個例子中,你的第一個異步調用沒有被包裝,所以它沒有足夠的「等待」來執行catchError中的expectAsync1。

+0

所以,包皮爲*變種CB = expectAsync1容易((E){}); *? – 2013-03-27 14:25:37

+0

是的。還有一些方法可以指定應該執行回調的頻率,但在這種情況下它應該沒有關係。 – 2013-03-27 17:47:30

+0

啊,我現在明白了。感謝Guillaume和@FlorianLoitsch! – 2013-03-27 19:17:04