'The argument type 'Future<UserCredential>' can't be assigned to the parameter type 'dynamic Function()'

I am learning about TDD and practicing my test writing. I want to write a test for my login through Firebase. here is the test file:

import 'package:firebase/firebase.dart';
import 'package:firebase_auth/firebase_auth.dart' as fa;
import 'package:flutter_test/flutter_test.dart';

import 'package:mocktail/mocktail.dart';
import 'package:scopetik/features/login/data/login_data_impl.dart';
import 'package:scopetik/features/login/models/user_model.dart';

class MockFirebaseAuth extends Mock implements fa.FirebaseAuth {}

class MockFirebaseUser extends Mock implements fa.User {}

class MockAuthResult extends Mock implements fa.UserCredential {}

void main() {
  late LoginDataImpl sut;
  late MockFirebaseAuth mockFirebaseAuth;

  setUp(() {
    mockFirebaseAuth = MockFirebaseAuth();
    sut = LoginDataImpl(mockFirebaseAuth);
  });

  group(
    'Test LoginDataImpl class',
    () {
      when(mockFirebaseAuth.signInWithEmailAndPassword(
              email: 'email', password: 'password'))
          .thenAnswer((_) async {
        return MockAuthResult();
      });
      test(
        "get UserModel",
        () async {
          //arrange
        },
      );
    },
  );
}

I don't know why the when function won't let me return a future<UserCredential>.



Solution 1:[1]

I also had the same problem. You need to use mockito instead of mocktail in order to do that.

You can find when property of both libraries here, mocktail, mockito

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Karthick T. Sharma