I have a stateful widget whose state depends on a async firebase database read call. While the data is being fetched, I display a waiting circular indicator. Once, data fetch is finished, I display the actual ListView widget.
I want to write a widget test to test this functionality. How do i set the value of isReadComplete = true in my test? Or How can I explicitly call success_callback() from my test?
bool isReadComplete;
App(){
isReadComplete = false;
firestore.collection('collection').document('doc').get().then((doc) {
success_callback(doc.data);
}).catchError((error) => {});
}
void success_callback(var data){
setState((){
isReadComplete = true;
})
}
@override
Widget build(BuildContext context) {
if (!isReadComplete) {
return new CircularIndicator();
} else {
List<Widget> widgetList = [];
return Scaffold(
body: ListView(
padding: EdgeInsets.all(8.0),
children: widgetList,
));
}
}
// Test for CircularProgressIndicator
testWidgets("Check CircularProgressIndicator", (WidgetTester tester) async {
await tester.pumpWidget(new MaterialApp(home: App));
// This works fine since isReadComplete = false
expect(find.byType(CircularProgressIndicator), findsOneWidget);
});
// Test for ListView
testWidgets("Check ListView", (WidgetTester tester) async {
await tester.pumpWidget(new MaterialApp(home: App));
// How do I do this?
// How to call success_callback()?
// How to set isReadComplete = true?
expect(find.byType(ListView), findsOneWidget);
});
After pumpWidget()
, you'd need to call pump()
with a Duration set to listen if the Future finishes within the set amount of time. An alternative to pump()
is pumpAndSettle()
- which calls pump
at least once and may repeatedly call pump
within a given duration.
await tester.pumpWidget(new MaterialApp(home: App));
await tester.pumpAndSettle();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.