我正在尝试测试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中调用另一个测试,但这有关系吗?
在包装的方法返回之前,是否期望ExpectAsync运行?
最佳答案
您需要致电callback
httpRequest.onLoadEnd.listen((ee) => callback()); // callback() instead of callback
callback
仅返回闭包引用。关于unit-testing - DART-ExpectAsync不适用于httpRequest,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22562222/