Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ApplicationsDocumentsDirectory return null for unit test?

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"

like image 929
vb10 Avatar asked May 15 '19 22:05

vb10


1 Answers

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 ".";
});

And now it finally works. Hope this helps.

like image 149
Mansehr Avatar answered Sep 24 '22 12:09

Mansehr