与飞镖异步测试奇怪的行为

问题描述:

为什么不这项工作?与飞镖异步测试奇怪的行为

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()必须在以后的测试中被回调。

这与测试框架的问题吗?这类问题不应该被默默地忽略。

任何异步调用应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