I'am tryng to run my unit test for this project, and it's giving me this error message:
MissingPluginException(No implementation found for method getApplicationDocumentsDirectory on channel plugins.flutter.io/path_provider)
package:flutter/src/services/platform_channel.dart 332:7 MethodChannel._invokeMethod
LateInitializationError: Field 'todoBox' has not been initialized.
test\unit_test\todo_unit_test.dart todoBox
test\unit_test\todo_unit_test.dart 35:13 main.<fn>
How can I solve this error?
** Here is the code: **
import 'dart:io';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hive/hive.dart';
import 'package:mocktail/mocktail.dart';
import 'package:path_provider/path_provider.dart';
import 'package:todo_app/src/modules/adapter/todo_adapter.dart';
late TodoItemMock todoItemMock;
late Box<TodoItemMock> todoBox;
class TodoItemMock extends Mock implements TodoItem {}
class HiveConfig {
static start() async {
Directory dir = await getApplicationDocumentsDirectory();
Hive.init(dir.path);
}
}
void main() {
WidgetsFlutterBinding.ensureInitialized();
setUp(
() async {
await HiveConfig.start();
Hive.registerAdapter(TodoItemAdapter());
todoBox = await Hive.openBox<TodoItemMock>('todoBox');
},
);
tearDownAll(
() async {
await todoBox.close();
},
);
test('Get no results from the Box', () async {
expect(todoBox.get(0), completion(findsNothing));
});
}
I tried to make a Mock for the path provider but it's not possible to call the class to mock it
Instead of mocking the path_provider try to mock the method channel that the path_provider wants to access.
Before the test set:
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(
const MethodChannel('plugins.flutter.io/path_provider'),
(MethodCall methodCall) async => 'test',);