Why does ApplicationsDocumentsDirectory return null for unit test?
Asked Answered
P

4

12

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"

Passivism answered 15/5, 2019 at 22:58 Comment(0)
D
29

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.

Ddene answered 13/10, 2019 at 23:13 Comment(2)
I want to hug youTillio
I also had to add TestWidgetsFlutterBinding.ensureInitialized()Zobkiw
P
4

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();
    });
}

Phelia answered 10/6, 2022 at 10:51 Comment(0)
W
2

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.

Worriment answered 30/11, 2022 at 7:55 Comment(0)
T
-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
Traditionalism answered 23/8, 2019 at 15:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.