Sub topic 13
Testing Code That Uses Storage
This chapter assumes you know Dart classes, Future, async and await, and how to add Flutter packages. You should also understand the basic repository pattern. A repository is a class that hides where data is stored and exposes operations such as loadDarkMode or saveNotes.
Storage code is often asynchronous and depends on a device directory, database, or platform API. Testing the screen directly against that storage makes tests slow and difficult to repeat. Instead, put storage behind a small interface and test the interface with an in-memory implementation.
Separate the app from storage
The app should depend on an abstraction, not directly on SharedPreferences or SQLite.
abstract interface class SettingsStore {
Future<bool> loadDarkMode();
Future<void> saveDarkMode(bool value);
}
class PreferencesSettingsStore implements SettingsStore {
PreferencesSettingsStore(this._prefs);
final SharedPreferences _prefs;
@override
Future<bool> loadDarkMode() async {
return _prefs.getBool('dark_mode') ?? false;
}
@override
Future<void> saveDarkMode(bool value) async {
await _prefs.setBool('dark_mode', value);
}
}The SettingsStore interface describes what the application needs. PreferencesSettingsStore describes one way to provide it. A widget can receive a SettingsStore in its constructor or through dependency injection, which means giving an object its dependencies from outside.
This separation gives you two choices:
- Use the real preferences implementation in the application.
- Use a fake implementation in a unit test.
Test preferences without a device
The shared_preferences package provides an in-memory mock. It behaves like preferences without writing to a phone, emulator, or desktop directory.
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test('loads a saved dark mode value', () async {
SharedPreferences.setMockInitialValues({'dark_mode': true});
final prefs = await SharedPreferences.getInstance();
final store = PreferencesSettingsStore(prefs);
expect(await store.loadDarkMode(), isTrue);
});
test('returns false when no value exists', () async {
SharedPreferences.setMockInitialValues({});
final prefs = await SharedPreferences.getInstance();
final store = PreferencesSettingsStore(prefs);
expect(await store.loadDarkMode(), isFalse);
});
}setMockInitialValues resets the in-memory data for a test. Reset it in every test so that one test cannot affect another. The expect function checks the result. isTrue and isFalse are matchers supplied by flutter_test.
Test a repository with a fake
For application logic, a small fake can be clearer than a package mock. This fake keeps values in a map.
class FakeSettingsStore implements SettingsStore {
bool darkMode = false;
@override
Future<bool> loadDarkMode() async => darkMode;
@override
Future<void> saveDarkMode(bool value) async {
darkMode = value;
}
}
void main() {
test('saving dark mode changes the stored value', () async {
final store = FakeSettingsStore();
await store.saveDarkMode(true);
expect(await store.loadDarkMode(), isTrue);
});
}A fake tests your application behavior, while the preferences test checks the adapter that talks to the package. Keep those responsibilities separate.
Choose the right test
| Test kind | Storage used | Main purpose |
|---|---|---|
| Unit test | Fake or in-memory mock | Check application logic quickly |
| Adapter test | Package mock | Check mapping and storage calls |
| Integration test | Temporary or real platform storage | Check platform behavior |
For SQLite repositories, fake the repository when testing widgets and business rules. Test SQL separately with a temporary database. Verify inserts, queries, migrations, and deletes, including empty results and invalid input.
Exercise
Why should a widget depend on a SettingsStore interface instead of constructing SharedPreferences itself?
Show answer
The interface lets the widget use a fast fake during tests and keeps platform-specific storage code outside the widget.