在我的代码中,该函数在一分钟后向流发出值。假设它是一个计时器。我想做单元测试(不是小部件测试,因为该函数存在于 bloc 文件中)。我尝试按照文档https://api.flutter.dev/flutter/quiver.testing.async/FakeAsync-class.html 中的描述使用 fakeAsync但没有运气。超时测试失败。经测试的代码:
class BarcodeBloc {
Timer _timer;
StreamController<bool> _timerFinished;
BarcodeBloc() {
_timerFinished = new StreamController();
}
Stream<bool> get cameraTimeout => _timerFinished.stream;
void _tick() {
_timerFinished.add(true);
}
void startTimer() {
stopTimer();
print("starting timer");
_timer = Timer(interval, _tick);
}
void stopTimer() {
if (_timer != null) {
_timer.cancel();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的测试代码:
void main() {
test("After one minute emits true", () {
new FakeAsync().run((async) {
BarcodeBloc barcodeBloc = new BarcodeBloc(preferenceProvider);
barcodeBloc.startTimer();
async.elapse(duration);
expect(barcodeBloc.cameraTimeout, emits(true));
});
}
Run Code Online (Sandbox Code Playgroud) 我使用 flutter_driver 在 Flutter 中进行集成测试。在某些屏幕上,按钮上的文本会出现溢出错误。我想创建一个可以显示溢出发生的测试。因此,当我在一堆虚拟/真实设备上运行所有集成测试时,我可以看到 UI 没有正确定位。