1

I'm trying to mock the background_downloader package , specifically this line in my class I'm trying to mock:

var status = await fileDownloader.permissions.status(permissionType);

I keep getting error message:

<type 'Future' is not a subtype of type 'Permissions'>


Here is my Test class:

class MockFileDownloader extends Mock implements FileDownloader {}

void main() {
  late MockFileDownloader mockFileDownloader;

  setUp(() {});

  tearDown(() {});

  group('BkgrdFileTransferBloc : _initBackgroundFileTransfer() ', () {
    setUp(() {
      SharedPreferences.setMockInitialValues({BackgroundFileTransfer.notificationDlgShownPref: false});

      mockFileDownloader = MockFileDownloader();

      var testBasedownloader = FileDownloader().downloaderForTesting;
      var permissionService = testBasedownloader.permissionsService;
      
      ///these lines here, I'm having issues with
      when(() => mockFileDownloader.permissions).thenReturn(permissionService);
      when(() => mockFileDownloader.permissions.status(PermissionType.notifications)).thenAnswer((_) async => PermissionStatus.denied);
      
    });
    blocTest(
      'should show notificationNotGrantedWarning - notificationDlgShownPref = false,   ',
      build: () {
        return BkgrdFileTransferBloc(mockFileDownloader);
      },
      act: (bloc) => bloc.add(BkgrdFilesTransferEvent.initialize(DownloadTask(url: ''))),
      expect:
          () => [
            TypeMatcher<BkgrdFileTransferState>()
                .having((bkgrdFileTransferState) => bkgrdFileTransferState.notificationNotGrantedWarning, 'notificationNotGrantedWarning', isTrue)
                .having((bkgrdFileTransferState) => bkgrdFileTransferState.task, 'task', isNotNull),
          ],
    );
  });
}

1 Answer 1

1

You can do

when(() => mockFileDownloader.permissions).thenReturn(permissionService);

but you cannot do

when(() => mockFileDownloader.permissions.status(PermissionType.notifications)).thenAnswer((_) async => PermissionStatus.denied);

because mockFileDownloader.permissions is already a permissionService. You would need to also create a mock of the permission service, mock its status method and then return it from the mockFileDownloader.permissions mock.

class MockPermissionsService extends Mock implements PermissionsService {}

final permissionsService = MockPermissionsService();
when(() => permissionsService.status(PermissionType.notifications)).thenAnswer((_) async => PermissionStatus.denied);

when(() => mockFileDownloader.permissions).thenReturn(permissionService);
Sign up to request clarification or add additional context in comments.

1 Comment

I think thats the problem ......the 3rd party package: "background_downloader" does not export / expose "PermissionsService" class so I cannot mock it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.