class MovieRemoteDataSourceImpl implements MovieDataSource {
final Dio dio;
MovieRemoteDataSourceImpl(this.dio);
@override
Future<Either<Exception, List<MovieResponse>>> fetchMovies() async {
const url = Endpoint.getTopRatedMovies;
try {
final addHeaderInterceptor = AddHeaderInterceptor();
dio.interceptors.add(addHeaderInterceptor); // Error in this line when run unit test.
final response = await dio.get(url);
final mapFromJson = response.data['results'] as List;
final result = mapFromJson.map((e) {
return MovieResponse.fromJson(e);
}).toList();
return Right(result);
} on Exception catch (e) {
return Left(e);
}
}
}
My unit test class:
@GenerateNiceMocks([MockSpec<Dio>()])
void main() {
group('Fetch list of movie.', () {
MockDio mockDioClient = MockDio();
final dataSource = MovieRemoteDataSourceImpl(mockDioClient);
RequestOptions requestOptions = RequestOptions();
Response mockResponseSuccess = Response(
requestOptions: requestOptions,
statusCode: 200,
data: listOfMovieMock,
);
test('Fetch success', () async {
when(mockDioClient.get(any)).thenAnswer((_) async => mockResponseSuccess);
final actual = await dataSource.fetchMovies();
expect(actual.isRight(), true);
});
test('Fetch error: 404 Not found', () async {
when(mockDioClient.get(any)).thenAnswer(
(_) async => throw DioException(requestOptions: requestOptions),
);
final actual = await dataSource.fetchMovies();
expect(actual.isLeft(), true);
});
});
}
Then I get an error:
FakeUsedError: 'interceptors'
No stub was found which matches the argument of this method call:
interceptors
A fake object was created for this call, in the hope that it won't be ever accessed.
Here is the stack trace where 'interceptors' was called:
...
However, member 'add' of the created fake object was accessed.
Add a stub for MockDio.interceptors using Mockito's 'when' API.
Sorry for my English.
My real data source class:
My unit test class:
Then I get an error: