So, i'm trying to test my flutter app. Here is what i do
class MockSplashScreenBloc extends MockBloc<SplashScreenState>
implements SplashScreenBloc {}
void main() {
MockSplashScreenBloc splashScreenBloc;
Widget MyWidget() {
return MaterialApp(
home: BlocProvider(
create: (context) {
return SplashScreenBloc(url: "google.com");
},
child: SplashScreen(),
),
);
}
group('Splash Screen Widget Test', () {
setUp(() {
splashScreenBloc = MockSplashScreenBloc();
});
tearDown(() {
splashScreenBloc?.close();
});
testWidgets('should render Container when state is Default State',
(WidgetTester tester) async {
when(splashScreenBloc.state).thenAnswer((_) => Default());
await tester.pumpWidget(MyWidget());
expect(find.byKey(ValueKey("container_empty")), findsOneWidget);
});
testWidgets('should render LoadingIndicator when state is Loading State',
(WidgetTester tester) async {
when(splashScreenBloc.state).thenReturn(LoadingState());
await tester.pumpWidget(MyWidget());
expect(find.byKey(ValueKey("splash_loading_bar")), findsOneWidget);
});
});
}
Here is my SplashScreen
class SplashScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: BlocBuilder<SplashScreenBloc, SplashScreenState>(
builder: (context, state) {
if (state is LoadingState) {
return CircularProgressIndicator(
key: Key("splash_loading_bar"),
);
} else if (state is NotConnected) {
return Text("Could not connect to server",
key: ValueKey("splash_screen_not_connected"));
} else if (state is Connected) {
return Text(
"Connected",
key: Key("splash_screen_connected"),
);
} else {
return Container(key: Key("container_empty"));
}
},
),
),
);
}
}
i can't pass this test should render LoadingIndicator when state is Loading State
, i alrady try to use expect(find.byType(CircularProgressIndicator), findsOneWidget);
but it is still not working, here is the error
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════ The following TestFailure object was thrown running a test: Expected: exactly one matching node in the widget tree Actual: _KeyFinder:<zero widgets with key [<'splash_loading_bar'>] (ignoring offstage widgets)>
Which: means none were found but one was expected
how can i fix it ?
So, in conclusion, we can say that keys are a very good feature in flutter. It only works with the Stateful widget and the purpose to use it to store the value of the current state when modifying the collection. My Personal Notesarrow_drop_up
On tapping the button the swapTiles function gets activated then the swapping takes place. The onTap button is the smiley face at the bottom. Dart import 'package:flutter/material.dart';
The test fails because it finds too many widgets and doesn't know which one to tap. So I figured I'd give keys to my + buttons by wrapping them in a container like this:
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue. Sign up for free to subscribe to this conversation on GitHub .
Did you try await tester.pumpAndSettle()?
tester.pump()
Triggers a rebuild of the widget after a given duration.tester.pumpAndSettle()
Repeatedly calls pump with the given duration until there are no longer any frames scheduled. This essentially waits for all animations to complete.
Please try the following code :
testWidgets('should render LoadingIndicator when state is Loading State',
(WidgetTester tester) async {
when(splashScreenBloc.state).thenReturn(LoadingState());
await tester.pumpWidget(MyWidget());
await tester.pumpAndSettle();
expect(find.byKey(ValueKey("splash_loading_bar")), findsOneWidget);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With