Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working within widget tests? #106

Open
muzzah opened this issue Aug 9, 2024 · 2 comments
Open

Not working within widget tests? #106

muzzah opened this issue Aug 9, 2024 · 2 comments

Comments

@muzzah
Copy link

muzzah commented Aug 9, 2024

Hi

Running a widget test on MacOS seems to break the library. Is this library supported in a widget test run/environment?
The execution seems to just stop at time when setting data into the local storage

@muzzah
Copy link
Author

muzzah commented Aug 9, 2024

I have migrated away from this library as it doesnt seem to work in widget tests.
Now using https://pub.dev/packages/shared_preferences instead

@tuna-f1sh
Copy link

For others, this can be resolved by copying the mock test setup used by the path_provider dependency: https://github.com/flutter/packages/blob/main/packages/path_provider/path_provider/test/path_provider_test.dart

Eg:

Add 'path_provider', 'path_provider_platform_interface' and 'plugin_platform_interface' as dev_dependences.

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

  group('Config', () {
    setUp(() async {
      // crate temp folder
      PathProviderPlatform.instance = FakePathProviderPlatform();
    });
...

Where FakePathProviderPlatform is:

String kTemporaryPath = Directory.systemTemp.path;
String kApplicationSupportPath = kTemporaryPath;
String kDownloadsPath = kTemporaryPath;
String kLibraryPath = kTemporaryPath;
String kApplicationDocumentsPath = kTemporaryPath;
String kExternalCachePath = kTemporaryPath;
String kExternalStoragePath = kTemporaryPath;

class FakePathProviderPlatform extends Fake
    with MockPlatformInterfaceMixin
    implements PathProviderPlatform {
  @override
  Future<String?> getTemporaryPath() async {
    return kTemporaryPath;
  }

  @override
  Future<String?> getApplicationSupportPath() async {
    return kApplicationSupportPath;
  }

  @override
  Future<String?> getLibraryPath() async {
    return kLibraryPath;
  }

  @override
  Future<String?> getApplicationDocumentsPath() async {
    return kApplicationDocumentsPath;
  }

  @override
  Future<String?> getExternalStoragePath() async {
    return kExternalStoragePath;
  }

  @override
  Future<List<String>?> getExternalCachePaths() async {
    return <String>[kExternalCachePath];
  }

  @override
  Future<List<String>?> getExternalStoragePaths({
    StorageDirectory? type,
  }) async {
    return <String>[kExternalStoragePath];
  }

  @override
  Future<String?> getDownloadsPath() async {
    return kDownloadsPath;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants