Home > front end >  Error when using argument matcher in mocking methods in dart null safety
Error when using argument matcher in mocking methods in dart null safety

Time:10-27

I am getting the following error message when using argument matcher, any, when mocking a method in dart tests using mockito in a null safe dart code base. What steps need to be taken to fix this issue

error:

The argument type 'Null' can't be assigned to the parameter type 'int'.

Test code can be found here:

class MockNumberTriviaRepository extends Mock implements NumberTriviaRespository {}

void main() {
  late GetConcreteNumberTrivia usecase;
  late MockNumberTriviaRepository mockNumberTriviaRepository;

  setUp(() {
    mockNumberTriviaRepository = MockNumberTriviaRepository();
    usecase = GetConcreteNumberTrivia(mockNumberTriviaRepository);
  });

  const tNumber = 1;
  const tNumberTrivia = NumberTrivia(number: tNumber, text: "test");

  test('should get trivia for the number from repository', () async {
    //arrange
    when(mockNumberTriviaRepository.getConcreteNumberTrivia(any)).thenAnswer((_) async => const Right(tNumberTrivia));

    //act
    final result = await usecase.execute(tNumber);

    //assert
    // UseCase should simply return whatever was returned from the Repository
    expect(result, const Right(tNumberTrivia));
    // Verify that the method has been called on the Repository
    verify(mockNumberTriviaRepository.getConcreteNumberTrivia(tNumber));
    verifyNoMoreInteractions(mockNumberTriviaRepository);
  });
}

Implementation code can be found here:

abstract class NumberTriviaRespository {
  Future<Either<Failure, NumberTrivia>> getConcreteNumberTrivia(int number);
  Future<Either<Failure, NumberTrivia>> getRandomNumberTrivia();
}

abstract class Failure extends Equatable {
  const Failure([List properties = const <dynamic>[]]);
}

class GetConcreteNumberTrivia {
  final NumberTriviaRespository respository;

  const GetConcreteNumberTrivia(this.respository);

  Future<Either<Failure, NumberTrivia>> execute(int number) async {
    return await respository.getConcreteNumberTrivia(number);
  }
}

class NumberTrivia extends Equatable {
  final String text;
  final int number;

  const NumberTrivia({required this.text, required this.number});

  @override
  List<Object?> get props => [text, number];
}

CodePudding user response:

Mockito has issues with Dart Null-safety. Please see https://github.com/dart-lang/mockito/blob/master/NULL_SAFETY_README.md.

You can override the implementation of your mock class to support a null argument by following the recipes on the link above:

class MockNumberTriviaRepository extends Mock
    implements NumberTriviaRespository {
  @override
  Future<Either<Failure, NumberTrivia>> getConcreteNumberTrivia(int? number) =>
      super.noSuchMethod(Invocation.method(#getConcreteNumberTrivia, [number]),
          returnValue: Future.value(
              Right<Failure, NumberTrivia>(NumberTrivia(text: "", number: 1))));
}
  • Related