Skip to content

Feature Proposal: Synchronized API Mocking & Environment Testing for testing_app #2885

Description

@Ashish6298

Hi @ericwindmill and team,

I’d like to propose an enhancement to the testing_app sample. While the current sample does a great job demonstrating the fundamentals of unit, widget, and integration testing, developers in the community frequently struggle with testing applications that rely on complex, asynchronous API layers and environment variables.

I propose adding a clean, 1P-focused blueprint for testing HTTP lifecycles and environment synchronization.

The Problem
When scaling Flutter apps, developers often rely heavily on third-party packages to mock network requests or handle environment toggles (Staging vs. Production), which can bloat the codebase and introduce fragile tests. There is a lack of official, concise reference material on how to mock API responses, handle artificial latency, and test loading/error states purely using the core SDK and official packages (like package:http/testing.dart).

Proposed Solution
I would like to add a dedicated "Network & Environment" testing suite to testing_app that demonstrates the following:

Environment Injection Pattern: A lightweight configuration pattern to toggle between "Mock" and "Live" environments without heavy 3P dependencies.

API Mocking with 1P Tools: Utilizing MockClient from package:http/testing.dart to intercept and simulate synchronized request-response lifecycles.

State-driven Widget/Integration Tests: Adding tests that explicitly assert UI state changes when a simulated API request is in-flight, succeeds, or fails (e.g., simulating a SocketException or a 500 timeout).

Why this fits flutter/samples
Zero 3P Bloat: Relies strictly on Flutter/Dart core and the official http package.

High Value: API synchronization and offline/error state handling are critical developer journeys for >80% of production apps.

Minimal Footprint: This can be implemented in a small, self-contained feature directory within testing_app (e.g., <200 LOC for the feature logic), strictly adhering to the repository's preference for concise quickstarts.

Implementation Plan
If approved, my PR will include:

A simple PostRepository (or similar) that fetches data.

Dependency injection of the HTTP client to allow swapping in a MockClient.

Unit tests verifying the repository logic against mocked JSON responses.

Widget/Integration tests that pump the UI and verify loading spinners, error dialogs, and successful data rendering based on the mock environment.

Updates to the README.md explaining the mocking pattern.

Please let me know if this aligns with the current goals for testing_app, and if you'd be open to reviewing a PR for this!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions