9

I'm using flutter "path_provider" plugin. I needed an SQLite operation. My error test class doesn't find "getApplicationDocumentsDirectory" and return null. The application runs for simulator/real device any working no problem.

Looking for provider repository and test folder.I've tired test class example but the error persists.

  const MethodChannel channel =
      MethodChannel('plugins.flutter.io/path_provider');

  channel.setMockMethodCallHandler((MethodCall methodCall) async {
    log.add(methodCall);
    return response;
  });

  test('user save data', () async {
    var response = null;
//FIXME : directory return null
    final Directory directory = await getApplicationDocumentsDirectory();
    final model = UserWordInformation();
    model.word = word;
    model.know = 1;
    final result = await dbHelper.insert(model.toMap());
    expect(result, 1);
  });

I expect return path folder for the device.Some path : "/Users/vb/Library/Developer/CoreSimulator/Devices/C5B3C94C-C774-4D0E-A19C-97AAF11BD9E3/data/Containers/Data/Application/0508712B-A138-483A-921E-B5EAE6DF149F/Documents"

user438383
  • 5,716
  • 8
  • 28
  • 43
vb10
  • 641
  • 5
  • 14

4 Answers4

24

Maybe you have forgotten to initialize your response variable.

I had a similar issue with getApplicationDocumentsDirectory in one of my unit tests.

MissingPluginException(No implementation found for method getApplicationDocumentsDirectory on channel plugins.flutter.io/path_provider)

Added the following code to the unit test file:

const MethodChannel channel = MethodChannel('plugins.flutter.io/path_provider');
channel.setMockMethodCallHandler((MethodCall methodCall) async {
  return ".";
});

After flutter upgrate:

TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
    .setMockMethodCallHandler(
        const MethodChannel('plugins.flutter.io/path_provider'),
        (MethodCall methodCall) async {
  return '.';
});

And now it finally works. Hope this helps.

Ravindra S. Patil
  • 11,757
  • 3
  • 13
  • 40
Mansehr
  • 511
  • 3
  • 6
3

The above answers might be correct back then. But did not work for me. The path_provider package provides a good test example here.

Basically we have Fake the PathProviderPlatform.

class FakePathProviderPlatform extends Fake
    with MockPlatformInterfaceMixin
    implements PathProviderPlatform {
  @override
  Future<String?> getApplicationDocumentsPath() async {
    return 'your path';
  }

  @override
  Future<String?> getApplicationSupportPath() async {
    return 'your path';
  }
}

void main(){
   setUp(() async {
      PathProviderPlatform.instance = FakePathProviderPlatform();
    });
}

Ritesh Singh
  • 782
  • 9
  • 19
1

Anyone dealing with this issue more recently there is an updated method that is more like the original answer. This is from the flutter_test documentation.

  //   'Use tester.binding.defaultBinaryMessenger.setMockMethodCallHandler or '
  //   'TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler instead. '
  //   'This feature was deprecated after v2.1.0-10.0.pre.'
  // )
  void setMockMethodCallHandler(Future<dynamic>? Function(MethodCall call)? handler) {
    TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.setMockMethodCallHandler(this, handler);
  }

Here is the code I added to my unit test group:

const MethodChannel channel = MethodChannel('plugins.flutter.io/path_provider');
TestDefaultBinaryMessengerBinding.instance?.defaultBinaryMessenger
      .setMockMethodCallHandler(channel, (MethodCall methodCall) async {
    return ".";
  });

If you are running on mac you may have to use plugins.flutter.io/path_provider_macos for MethodChannel's argument.

DeeCe5531
  • 11
  • 2
-1

this problem usually happens due to lack of dependencies, your pubspec.yaml has the following dependencies?

dependencies:

  path_provider: ^1.2.0

  simple_permissions: ^0.1.9

if simple_permissions: ^0.1.9 throws errors at build time try instead this dependencies:

path_provider: ^1.2.0

permission_handler: ^3.2.0