Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to mock Dio.interceptors.add when write unit test? #2086

Closed
devnta opened this issue Jan 5, 2024 · 1 comment
Closed

How to mock Dio.interceptors.add when write unit test? #2086

devnta opened this issue Jan 5, 2024 · 1 comment

Comments

@devnta
Copy link

devnta commented Jan 5, 2024

Package

dio

Version

5.4.0

Operating-System

Android, iOS

Output of flutter doctor -v

No response

Dart Version

No response

Steps to Reproduce

Sorry for my English.

My real data source class:

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.

Expected Result

My unit test is passed.

Actual Result

Occur an error.

@devnta devnta added h: need triage This issue needs to be categorized s: bug Something isn't working labels Jan 5, 2024
@devnta
Copy link
Author

devnta commented Jan 5, 2024

For everyone facing the same issue, just add:

final fakeInterceptors = Interceptors();
when(mockDioClient.interceptors).thenReturn(fakeInterceptors);

@devnta devnta closed this as completed Jan 5, 2024
@AlexV525 AlexV525 removed h: need triage This issue needs to be categorized s: bug Something isn't working labels Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants