Bacancy Technology
Bacancy Technology represents the connected world, offering innovative and customer-centric information technology experiences, enabling Enterprises, Associates and the Society to Rise™.
12+
Countries where we have happy customers
1050+
Agile enabled employees
06
World wide offices
12+
Years of Experience
05
Agile Coaches
14
Certified Scrum Masters
1000+
Clients projects
1458
Happy customers
Artificial Intelligence
Machine Learning
Salesforce
Microsoft
SAP
November 15, 2024
> Using Future.delayed(Duration()) we can wait for specific duration.
We create a custom class in which we implement timer features.
import 'dart:async'; class CustomTimer { int _seconds; bool _isRunning = false; Timer? _timer; CustomTimer(this._seconds); int get seconds => _seconds; void start() { _isRunning = true; _timer = Timer.periodic(const Duration(seconds: 1), (timer) { if (_seconds > 0) { _seconds--; } else { _timer?.cancel(); _isRunning = false; } }); } }
Now we add unit test related code and use this CustomTimer class inside unit test file.
import 'dart:async'; import 'package:flutter_test/flutter_test.dart'; import '../test_class/custom_timer.dart'; void main() { group('Timer Tests', () { test("Testing timer", () async { CustomTimer timer = CustomTimer(10); // Start with 10 seconds int startTime = timer.seconds; timer.start(); // Wait for 2 seconds await Future.delayed(const Duration(seconds: 2)); // Check if the timer's seconds are within an expected range expect(timer.seconds, inInclusiveRange(startTime - 2, startTime - 1)); }); }); }
In above example, we can expect seconds is inInclusiveRange(startTime – 2 , startTime – 1) after delaying 2 seconds.
admin@Mac-mini-3 testing % flutter test
00:01 +1: All tests passed!