gpt4 book ai didi

unit-testing - 如何将 FakeAsync 与 testWidgets 一起使用?

转载 作者:行者123 更新时间:2023-12-03 03:21:55 31 4
gpt4 key购买 nike

我正在尝试使用 Dart quiver 库中的 FakeAsync 来模拟 CountDownTimer 的时钟。但是,当我在 run 方法中等待任何异步函数时,它会挂起。

testWidgets('Resend OTP', (WidgetTester tester) async {
await FakeAsync().run((async) async {
final countDownTimer = new CountdownTimer(
new Duration(seconds: 30),
new Duration(seconds: 1),
stopwatch: FakeStopwatch(
() => async.getClock(DateTime.fromMillisecondsSinceEpoch(0)).now().millisecondsSinceEpoch, 1000),
);
print(countDownTimer.remaining);
print(countDownTimer.isRunning);
await tester.pumpWidget(Container());

});
});

最佳答案

找到了,无论如何,我都会在这里发布它,以防将来有人需要答案:

testWidgets('Resend OTP Test concept', (WidgetTester tester) async {
final fakeAsync = FakeAsync();
fakeAsync.run((async) async {
final countDownTimer = new CountdownTimer(
new Duration(seconds: 30),
new Duration(seconds: 1),
stopwatch: FakeStopwatch(
() => async.getClock(DateTime.fromMillisecondsSinceEpoch(0)).now().millisecondsSinceEpoch, 1000),
);
print(countDownTimer.remaining);
print(countDownTimer.isRunning);
await tester.pumpWidget(Container());
// expect(true, false);

});
fakeAsync.flushMicrotasks();
});

诀窍是为 FakeAsync 创建一个变量,然后同步调用 fakeAsync.flushMicrotasks();

关于unit-testing - 如何将 FakeAsync 与 testWidgets 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62164678/

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