I am encountering an issue when testing my Flutter widget, CitySearchButton
. I want to verify that a CircularProgressIndicator
is displayed when isLoading
is true, but the test fails.
Problem Description:
I am testing the CitySearchButton
widget, and I want to confirm that a CircularProgressIndicator
is displayed when isLoading
is true. However, the test does not pass.
Reproducible Code:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:my_app/city_search_button.dart';
import 'package:mockito/mockito.dart';
import 'package:provider/provider.dart';
class MockViewModel extends Mock implements ViewModel {}
void main() {
testWidgets('Displays CircularProgressIndicator when isLoading is true', (WidgetTester tester) async {
// Setting up the Mock ViewModel
final mockViewModel = MockViewModel();
when(mockViewModel.state).thenReturn(CitySearchState(isLoading: true));
// Setting up the Provider Container
final container = ProviderContainer(
overrides: [
viewModelProvider.overrideWithValue(mockViewModel),
],
);
// Building the widget under test
await tester.pumpWidget(UncontrolledProviderScope(
container: container,
child: const MaterialApp(
home: Scaffold(
body: CitySearchButton(),
),
),
));
// Pumping again to reflect the state
await tester.pump();
// Debugging the widget tree
debugDumpApp();
// Verifying that CircularProgressIndicator is displayed
expect(find.byType(CircularProgressIndicator), findsOneWidget);
});
}
What I Have Tried:
- Used
pumpAndSettle()
, but the result did not change. - Used
debugDumpApp()
to debug the widget tree, but the expected widget was not found.
Error Message:
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure was thrown running a test:
Expected: exactly one matching candidate
Actual: _TypeWidgetFinder:<Found 0 widgets with type "CircularProgressIndicator": []>
Which: means none were found but one was expected
When the exception was thrown, this was the stack:
#4 main.<anonymous closure>.<anonymous closure> (file:///Users/username/Flutter/weather_app/test/components/city_search_button_test.dart:64:7)
<asynchronous suspension>
#5 testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:168:15)
<asynchronous suspension>
#6 TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:1013:5)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)
This was caught by the test expectation on the following line:
# file:///Users/username/Flutter/weather_app/test/components/city_search_button_test.dart line 64
The test description was:
Displays CircularProgressIndicator when isLoading is true
════════════════════════════════════════════════════════════════════════════════════════════════════
Mock ViewModel Setup:
class MockViewModel extends Mock implements ViewModel {}
final mockViewModel = MockViewModel();
when(mockViewModel.state).thenReturn(CitySearchState(isLoading: true));
Provider Container Setup:
final container = ProviderContainer(
overrides: [
viewModelProvider.overrideWithValue(mockViewModel),
],
);
Environment:
- Flutter 3.16.3
Additional Information:
- flutter_riverpod: ^2.5.1
- mockito: ^5.4.4
Any help would be greatly appreciated. Thank you!
What Did You Try?
- I used
pumpAndSettle()
to ensure all animations and state changes were settled before checking for theCircularProgressIndicator
. - I used
debugDumpApp()
to inspect the widget tree and confirmed that the expected state was not reflected.
What Were You Expecting?
- I expected
CircularProgressIndicator
to be present in the widget tree whenisLoading
is set to true in theCitySearchState
. - I expected the
debugDumpApp()
output to include theCircularProgressIndicator
widget, indicating that the loading state was correctly handled by theCitySearchButton
.
Itsuking is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.