Hi, today I've been trying to write some unit tests using Mocked Permission and I've run into that kind of error: MissingPluginException(No implementation found for method requestPermissions on channel flutter.baseflow.com/permissions/methods).
I tried reproduce the error on a simpler example but instead I've encountered different one: type 'Future<PermissionStatus>' is not a subtype of type 'int'.
I don't actually know if I'm doing something wrong or this is an error so if you could help me with that I'd be greatful.
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:permission_handler/permission_handler.dart';
class MockPermission extends Mock implements Permission {}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final mockPermission = MockPermission();
test("Test", () async {
when(mockPermission.request()).thenAnswer((_) async => PermissionStatus.denied);
final result = await mockPermission.request();
});
}
after running the test:
Testing started at 12:21 ...
/Users/michal/Develop/flutter/bin/flutter --no-color test --machine test/src/repositories/permission_test.dart
package:permission_handler_platform_interface/src/permissions.dart 16:13 MockPermission.value
package:permission_handler_platform_interface/src/method_channel/utils/codec.dart 27:39 Codec.encodePermissions.<fn>
dart:_internal ListIterable.toList
package:permission_handler_platform_interface/src/method_channel/utils/codec.dart 27:46 Codec.encodePermissions
package:permission_handler_platform_interface/src/method_channel/method_channel_permission_handler.dart 64:24 MethodChannelPermissionHandler.requestPermissions
dart:async _AsyncAwaitCompleter.start
package:permission_handler_platform_interface/src/method_channel/method_channel_permission_handler.dart 62:63 MethodChannelPermissionHandler.requestPermissions
package:permission_handler/permission_handler.dart 105:16 PermissionListActions.request
package:permission_handler/permission_handler.dart 45:26 PermissionActions.request
dart:async _AsyncAwaitCompleter.start
package:permission_handler/permission_handler.dart 44:35 PermissionActions.request
test/src/repositories/permission_test.dart 13:41 main.<fn>
dart:async _AsyncAwaitCompleter.start
test/src/repositories/permission_test.dart 11:16 main.<fn>
type 'Future<PermissionStatus>' is not a subtype of type 'int'
Same here!!!, any solutions?
type 'Future<bool>' is not a subtype of type 'int'
package:permission_handler_platform_interface/src/permissions.dart 16:13 PermissionMock.value
package:permission_handler_platform_interface/src/method_channel/utils/codec.dart 27:39 Codec.encodePermissions.<fn>
dart:_internal ListIterable.toList
package:permission_handler_platform_interface/src/method_channel/utils/codec.dart 27:46 Codec.encodePermissions
package:permission_handler_platform_interface/src/method_channel/method_channel_permission_handler.dart 64:24 MethodChannelPermissionHandler.requestPermissions
package:permission_handler/permission_handler.dart 109:16 PermissionListActions.request
package:permission_handler/permission_handler.dart 49:26 PermissionActions.request
test/services/permission_service_test.dart 33:45 main.<fn>.<fn>
test/services/permission_service_test.dart 24:9 main.<fn>.<fn>
EDIT :
I think the problem is that the request() method is an Extension method, and according to Mockito issue #239 Mocking Extension methods can't be done.
I think this is an issue related to the Mocking library that you are using _( Mockito in your case )_.
@mhnowak I think you'll find the answer in #262, don't try to mock the class just mock the behavior.
Hi @mhnowak
Did you get it working?
If so, and you didn't find any faulty behaviour in your original approach, I think we can close the issue!
Hi try to do this :
assume that you need a microphone permission.
class PermissionHandler {
Future<PermissionStatus> requestMicrophonePermission() =>
Permission.microphone.request();
}
class SomeClass {
PermissionHandler _permissionHandler;
SomeClass(PermissionHandler permissionHandler)
: _permissionHandler = permissionHandler ?? PermissionHandler();
...
}
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
class MockPermissionHandler extends Mock implements PermissionHandler {}
void main() {
PermissionHandler permissionHandler;
SomeClass someClass;
setUp(() {
permissionHandler = MockPermissionHandler();
someClass = SomeClass(permissionHandler);
});
test('Should do something', () {
when(permissionHandler.requestMicrophonePermission())
.thenAnswer((_) => Future.value(PermissionStatus.granted));
});
}
You can't mock an extension method because mockito does not support . I hope this will help you.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.
To anyone experiencing this and needs an easy way to mock use this solution
https://github.com/Baseflow/flutter-permission-handler/issues/262#issuecomment-767834250 handles it perfecly
Most helpful comment
Hi try to do this :
assume that you need a microphone permission.
You can't mock an extension method because mockito does not support . I hope this will help you.