DART - expectAsync不适用于httpRequest

时间:2014-03-21 14:58:10

标签: unit-testing dart

我试图测试在httpRequest返回后是否发生异步调用。

这是我的代码:

...       
   var httpRequest = new HttpRequest();
   httpRequest.withCredentials = true;
   httpRequest.open('POST', repositoryURL);
   httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   httpRequest.setRequestHeader("Authorization", auth);
   test('Asynch testing of getting Repositories', (){
     var callback =  expectAsync(() {repositoryListComplete(httpRequest);});
     httpRequest.onLoadEnd.listen((ee) => callback);     
     httpRequest.onError.listen((ee) => handleError(ee));
     httpRequest.send('');
   });
}

void repositoryListComplete(HttpRequest request){
    print('Testing URL connection for ${repositoryURL}');
...

测试停止,似乎永远不会调用回调。

如果我只是使用repositoryListComplete作为onLoadEnd的回调,那么它就会被调用而没有任何问题。

我在这里发现了类似的错误报告:

https://code.google.com/p/dart/issues/detail?id=16677

我只是想知道我是否有同样的问题或我的代码不正确?

编辑: - 我将代码更改为

   httpRequest.onLoadEnd.listen((ee) => callback());    

现在我得到了这个:

FAIL
1   PASS    Expectation: Checking if all UI components are present.
2   FAIL    Expectation: Asynch testing of getting Repositories. Caught Bad state: Not allowed when tests are running.
package:unittest/unittest.dart 268:21               test
documentviewertest.dart 275:9                       repositoryListComplete
documentviewertest.dart 261:60                      sendRepositoryListRequest.<fn>.<fn>
package:unittest/src/spread_args_helper.dart 94:23  invoke0
documentviewertest.dart 262:51                      sendRepositoryListRequest.<fn>.<fn>
dart:async                                          _BaseZone.bindUnaryCallback.<fn>
Total 1 passed, 1 failed 0 errors

我在repositoryListComplete中调用另一个测试,但是它应该重要吗?

在wrap方法返回之前,expectAsync是否被认为是运行的?

1 个答案:

答案 0 :(得分:0)

您需要致电callback

httpRequest.onLoadEnd.listen((ee) => callback());  // callback() instead of callback

callback只返回闭包引用。