gpt4 book ai didi

unit-testing - DART - expectAsync 不适用于 httpRequest

转载 作者:行者123 更新时间:2023-12-03 04:07:09 25 4
gpt4 key购买 nike

我正在尝试测试 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com