/// A flag that indicates whether the NotificationProvider is being processed.
bool _isProcess = false;
set isProcess(bool value) {
_isProcess = value;
notifyListeners();
}
bool get isProcess => _isProcess;
NotificationProvider({required this.fetchNotificationUseCase});
//TODO("Satoshi"): catch exception or error
/// Fetch notification from api-server.
Future<void> fetchNotification() async {
try {
isProcess = true;
final res = await fetchNotificationUseCase.fetchNotification();
notificationList.clear();
notificationList.addAll(res);
isProcess = false;
notifyListeners();
} catch (e) {
isProcess = false;
notifyListeners();
debugPrint(e.toString());
}
}
I want to test isProcess property is changed to true and false. But the only I can test is just isProcess is false after fetchNotification method is completed.
How to test isProcess’s change ?
This is a test code what I wrote
test('then isProcess is being false', () async {
await notificationProvider.fetchNotification();
expect(notificationProvider.isProcess, false);
});
2
Answers
You can watch value changing using
addListener
method inChangeNotifier
.sample code
fetchNotification
synchronously setsisProcess = true
before doing asynchronous work. You therefore can synchronously check thatnotificationProvider.isProcess
transitions fromfalse
totrue
and then check that it returns tofalse
after all asynchronous work completes: