import 'dart:convert'; import 'package:{{ name }}/features/login/data/datasources/login_remote_datasource.dart'; import 'package:{{ name }}/features/login/data/models/token_model.dart'; import 'package:{{ name }}/features/login/data/services/login_service.dart'; import 'package:dio/dio.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/mockito.dart'; import '../../../../fixtures/fixture_reader.dart'; class MockLoginService extends Mock implements LoginService {} void main() { MockLoginService mockLoginService = MockLoginService(); LoginRemoteDataSourceImpl dataSource = LoginRemoteDataSourceImpl(api: mockLoginService); group('loginUser', () { const String tEmail = 'test@test.com'; const String tPassword = 'test'; const TokenModel tTokenModel = TokenModel(accessToken: '1234'); test('should return the User object if the status code is 200', () async { //arrange when(mockLoginService.login( email: tEmail, password: tPassword, )).thenAnswer((_) async => TokenModel.fromJson(jsonDecode(fixture('user_login.json')))); //act final result = await dataSource.loginUser(tEmail, tPassword); //assert verify(mockLoginService.login( email: tEmail, password: tPassword, )); expect(result, equals(tTokenModel)); }); test('should throw an exception if the status code is not 200', () async { //arrange when(mockLoginService.login( email: tEmail, password: tPassword, )).thenThrow(DioError( type: DioErrorType.connectTimeout, requestOptions: RequestOptions(path: ""))); //act final call = dataSource.loginUser; //assert expect(() => call(tEmail, tPassword), throwsA(isA())); }); }); }