Unit testing 如何将FakeAsync与testWidgets一起使用?
我试图使用DartUnit testing 如何将FakeAsync与testWidgets一起使用?,unit-testing,dart,Unit Testing,Dart,我试图使用Dartquiver库中的FakeAsync来模拟倒计时的时钟。 但是,当我在run方法中等待任何异步函数时,它会挂起 testWidgets('Resend OTP', (WidgetTester tester) async { await FakeAsync().run((async) async { final countDownTimer = new CountdownTimer( new Duration(seconds: 30), new
quiver
库中的FakeAsync
来模拟倒计时的时钟。
但是,当我在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()
顺便说一句,testWidgets
已经在一个FakeAsync
区域内执行它的主体,您可以使用tester.pump()
提前它的时钟。但是如何使用该FakeAsync区域来控制这个倒计时计时器的时钟呢?