Feature Proposal: Synchronized API Mocking & Environment Testing for testing_app
- Dominant language
- Dart
- Stars
- 19.3k
- Forks
- 7.9k
- Avg merge
- 3h 26m
- Merged PRs (30d)
- 3
Description
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!
Contributor guide
Research direction
Start by reviewing the testing_app sample and its README.md, then examine package:http/testing.dart and MockClient. Define the proposed PostRepository, environment injection, and loading, success, and error test cases within a self-contained feature directory. Done means the sample includes mocked JSON and failure tests plus widget or integration coverage for each stated UI state.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart, flutter
- Domain
- api, mobile-dev, testing-qa
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100