easy_mock_path_provider 0.1.1
easy_mock_path_provider: ^0.1.1 copied to clipboard
Readable test mock for the path_provider method channel, built on easy_mock_channel. Provides default directory paths and simple overrides with no dependency on path_provider.
easy_mock_path_provider #
A readable test mock for path_provider
with default values for every directory lookup.
Built on easy_mock_channel, like the other channel-based Easy Mock helpers.
Mocks plugins.flutter.io/path_provider directly, with no dependency on the real
plugin.
This covers the method-channel implementation. Platform implementations using Pigeon, FFI, or other channel names are outside its scope. It does not change which implementation the plugin selects or bypass the plugin's platform checks.
Usage #
Add easy_mock_path_provider to your app's dev_dependencies.
import 'package:easy_mock_path_provider/easy_mock_path_provider.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:path_provider/path_provider.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
setUp(mockPathProvider.init);
test('uses default paths', () async {
final directory = await getApplicationDocumentsDirectory();
expect(directory.path, '/app_root/documents');
});
test('overrides paths', () async {
mockPathProvider.set(
temporaryPath: '/test/temp',
applicationDocumentsPath: '/test/documents',
);
expect((await getTemporaryDirectory()).path, '/test/temp');
expect((await getApplicationDocumentsDirectory()).path, '/test/documents');
});
}
init() resets all values and uses Easy Mock to remove the channel handler at
teardown. Call it from setUp or inside a test.
Default values #
Every path can be overridden with the corresponding named argument to set.
Omitted or null arguments retain the current value.
set argument |
Default value |
|---|---|
temporaryPath |
/app_root/tmp |
applicationSupportPath |
/app_root/support |
libraryPath |
/app_root/library |
applicationDocumentsPath |
/app_root/documents |
applicationCachePath |
/app_root/cache |
externalStoragePath |
/app_root/external/files |
externalCachePaths |
['/app_root/external/cache'] |
externalStoragePaths |
['/app_root/external/files'] |
downloadsPath |
/app_root/downloads |
getExternalStorageDirectories(type: StorageDirectory.downloads) appends
/downloads to each configured externalStoragePaths base path. Other storage
types append their enum name. Passing an empty list returns no directories.
The mock supplies paths only; it does not create directories or perform any filesystem operations. Tests that write files must provide a filesystem with the required directories, including any custom path overrides.
Loading and failure states #
mockPathProvider.delay(const Duration(milliseconds: 300));
mockPathProvider.throwError(Exception('storage unavailable'));
These apply to all mocked channel methods and reset on init(). Errors reach the
caller as PlatformExceptions. In widget tests, pump the configured duration to
advance delayed lookups.