gpt4 book ai didi

unit-testing - 我如何在Dart中的then语句中进行测试

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

我想对某些功能进行一些单元测试,但是我需要在Future完成后执行所有测试。

为了解决我的问题,这里是我想做的一个例子:

registerToServer(contentOfRequest).then((id){
test('test function1', () {
function1(id, contentOfRequest).then(expectAsync((val){
expect(val, whatIExpect);
}));
});
test('test function2', () {
function2(id, contentOfRequest).then(expectAsync((val){
expect(val, whatIExpect);
}));
}):
...
};

我尝试了一种替代解决方案,但这不会改变任何内容:

String id;
registerToServer(contentOfRequest).then((sid){
id = sid;
}).then((_){
test('test function1', () {
function1(id, contentOfRequest).then(expectAsync((val){
expect(val, whatIExpect);
}));
});
test('test function2', () {
function2(id, contentOfRequest).then(expectAsync((val){
expect(val, whatIExpect);
}));
}):
...
};

而且,如果可能的话,我将按这样组织测试,因为我想要所有测试的描述。

stacktrace是这样的:

Unhandled exception:
Uncaught Error: Bad state: Not allowed when tests are running.
Stack Trace:
#0 _requireNotRunning (package:unittest/unittest.dart:430:3)
#1 test (package:unittest/unittest.dart:100:21)
#2 main.<anonymous closure> (file:///.../server_test.dart:260:11)
#3 _RootZone.runUnary (dart:async/zone.dart:1155)
#4 _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:484)
#5 _Future._propagateToListeners (dart:async/future_impl.dart:567)
#6 _Future._completeWithValue (dart:async/future_impl.dart:358)
#7 _Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:412)
#8 _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41)
#9 _asyncRunCallback (dart:async/schedule_microtask.dart:48)
#10 _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:84)
#11 _startIsolate (dart:isolate-patch/isolate_patch.dart:244)
#12 _startMainIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:192)
#13 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:130)

#0 _rootHandleUncaughtError.<anonymous closure> (dart:async/zone.dart:886)
#1 _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41)
#2 _asyncRunCallback (dart:async/schedule_microtask.dart:48)
#3 _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:84)
#4 _startIsolate (dart:isolate-patch/isolate_patch.dart:244)
#5 _startMainIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:192)
#6 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:130)

最佳答案

我猜不支持像您在此处那样将测试包装到其他功能中

registerToServer(contentOfRequest).then((id){

test('test function1',(){

您可以使用 setUp()为测试做准备

main() {
group('xxx', () {
setUp(() {
return registerToServer(contentOfRequest);
});

test('some', () {
return function1(id, contentOfRequest).then((val){
expect(val, whatIExpect);
}));
});
});
}

缺点是当前无法为一组测试进行设置。每次测试之前/之后都会调用 setUp()tearDown()
请在 https://github.com/dart-lang/unittest/issues/18中添加评论(+1或类似的评论)

在setUp / tearDown / test中使用异步调用时,始终返回将来。测试框架识别何时返回了将来,并等待直到将来完成才结束。这样,您就不必面对 expectAsync或类似工具。

关于unit-testing - 我如何在Dart中的then语句中进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29102574/

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