Reputation: 7661
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.
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?
Upvotes: 0
Views: 268
Reputation: 420
I've tested your code and got same error as you. After making these changes everything runs fine, try it yourself.
class MockClient extends Mock implements GhibliFilms {
@override
Future<List<FilmModel>> getFilms() async {
return Future.value(null); // this is not that important
}
}
void main() {
final GhibliFilms ghibliMock = MockClient();
// async/await here was important
test('If API call was unsuccessful and data received is null', () async {
expect(await ghibliMock.getFilms(), null);
});
}
Upvotes: 1