Flutter Widget Test: CitySearchButton not behaving as expected

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 the CircularProgressIndicator.
  • 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 when isLoading is set to true in the CitySearchState.
  • I expected the debugDumpApp() output to include the CircularProgressIndicator widget, indicating that the loading state was correctly handled by the CitySearchButton.

New contributor

Itsuking is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật