samedi 23 janvier 2021

How to test nulls using mockito?

I am using mockito 4.1.3 , and here I have some test class:

import 'package:flutter_test/flutter_test.dart';
import 'package:ghinbli_app/models/film_model.dart';
import 'package:ghinbli_app/network/ghibli_films.dart';
import 'package:mockito/mockito.dart';

class MockClient extends Mock implements GhibliFilms {
  @override
  Future<List<FilmModel>> getFilms() async{
    return null;
}

void main() {
  final GhibliFilms ghibliMock = MockClient();
  test('If API call was unsuccessful and data received is null', () {
    expect(ghibliMock.getFilms(), null);
  });
}

Inside the MockClient class, I am overriding a method called getFilms() and returning null to simulate a situation when a call to some API returns null as data.


A problem

When I try to check if getFilms() actually returns a null value my test will fail with this error (probably because of the return type of getFilms()):

Expected: <null>
     Actual: <Instance of 'Future<List<FilmModel>>'>

How can I check and test that the data from getFilms() is actually null, what am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire