-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added some tests and got all tests passing again - DC
- Loading branch information
1 parent
2d53b16
commit 70f4af3
Showing
10 changed files
with
738 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
test/unit_tests/blocs/translation/translation_bloc_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import 'package:bloc_test/bloc_test.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:get_it/get_it.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:uchu/blocs/translation/translation_bloc.dart'; | ||
import 'package:uchu/services/translation_service.dart'; | ||
|
||
import '../../mocks.dart'; | ||
|
||
main() { | ||
late TranslationService mockTranslationService; | ||
late TranslationBloc testObject; | ||
const expectedTatoebaKey = 123; | ||
const expectedTranslation = 'the quick brown fox jumps over the lazy dog'; | ||
const expectedErrorMessage = 'something went wrong!'; | ||
|
||
setUp(() async { | ||
await GetIt.instance.reset(); | ||
mockTranslationService = MockTranslationService(); | ||
GetIt.instance | ||
.registerSingleton<TranslationService>(mockTranslationService); | ||
|
||
testObject = TranslationBloc(); | ||
}); | ||
|
||
blocTest( | ||
'emits TranslationLoading and TranslationLoaded when TranslationService.getSentenceFrom does not throw', | ||
setUp: () { | ||
when(() => mockTranslationService.getSentenceFrom( | ||
tatoebaKey: expectedTatoebaKey)) | ||
.thenAnswer((_) async => expectedTranslation); | ||
}, | ||
build: () => testObject, | ||
act: (bloc) => bloc.add( | ||
const TranslationFetchTranslationEvent( | ||
tatoebaKey: expectedTatoebaKey, | ||
), | ||
), | ||
expect: () => [ | ||
const TranslationLoading(), | ||
const TranslationLoaded(translation: expectedTranslation), | ||
]); | ||
|
||
blocTest( | ||
'emits TranslationLoading and TranslationError when TranslationService.getSentenceFrom does throw', | ||
setUp: () { | ||
when(() => mockTranslationService.getSentenceFrom( | ||
tatoebaKey: expectedTatoebaKey)).thenThrow(expectedErrorMessage); | ||
}, | ||
build: () => testObject, | ||
act: (bloc) => bloc.add( | ||
const TranslationFetchTranslationEvent( | ||
tatoebaKey: expectedTatoebaKey, | ||
), | ||
), | ||
expect: () => [ | ||
const TranslationLoading(), | ||
const TranslationError(errorString: expectedErrorMessage), | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:http/http.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:uchu/services/translation_service.dart'; | ||
|
||
import '../mocks.dart'; | ||
|
||
main() { | ||
late Client mockClient; | ||
late TranslationService testObject; | ||
|
||
setUp(() { | ||
mockClient = MockClient(); | ||
testObject = TranslationService(client: mockClient); | ||
}); | ||
|
||
group( | ||
'getSentenceFrom', | ||
() { | ||
const expectedTatoebaKey = 1; | ||
const expectedTranslation = 'translation'; | ||
const goodResponseBody = | ||
'{"translations":[[{"lang":"eng","text":"$expectedTranslation"}]]}'; | ||
const noEngTranslationResponseBody = | ||
'{"translations":[[{"lang":"ger","text":"Übersetzung"}]]}'; | ||
|
||
test( | ||
'throws HttpException if tatoeba response is < 200', | ||
() async { | ||
when(() => mockClient.get(Uri.parse( | ||
'https://tatoeba.org/en/api_v0/sentence/$expectedTatoebaKey'))) | ||
.thenAnswer( | ||
(_) async => Response('error', 199), | ||
); | ||
|
||
expect( | ||
() async => await testObject.getSentenceFrom(tatoebaKey: 1), | ||
throwsA(isA<HttpException>()), | ||
); | ||
}, | ||
); | ||
|
||
test( | ||
'throws HttpException if tatoeba response is >299', | ||
() async { | ||
when(() => mockClient.get(Uri.parse( | ||
'https://tatoeba.org/en/api_v0/sentence/$expectedTatoebaKey'))) | ||
.thenAnswer( | ||
(_) async => Response('error', 300), | ||
); | ||
|
||
expect( | ||
() async => await testObject.getSentenceFrom(tatoebaKey: 1), | ||
throwsA(isA<HttpException>()), | ||
); | ||
}, | ||
); | ||
test( | ||
'does not throw an exception if tatoeba response is 200 and there is an english translation', | ||
() async { | ||
when(() => mockClient.get(Uri.parse( | ||
'https://tatoeba.org/en/api_v0/sentence/$expectedTatoebaKey'))) | ||
.thenAnswer( | ||
(_) async => Response(goodResponseBody, 200), | ||
); | ||
|
||
expect(await testObject.getSentenceFrom(tatoebaKey: 1), | ||
expectedTranslation); | ||
|
||
verify(() => mockClient.get(Uri.parse( | ||
'https://tatoeba.org/en/api_v0/sentence/$expectedTatoebaKey'))) | ||
.called(1); | ||
}, | ||
); | ||
|
||
test( | ||
'does throw an exception if tatoeba response is 200 but there is not an english translation', | ||
() async { | ||
when(() => mockClient.get(Uri.parse( | ||
'https://tatoeba.org/en/api_v0/sentence/$expectedTatoebaKey'))) | ||
.thenAnswer( | ||
(_) async => Response(noEngTranslationResponseBody, 200), | ||
); | ||
|
||
expect( | ||
() async => await testObject.getSentenceFrom(tatoebaKey: 1), | ||
throwsA(isA<Exception>()), | ||
); | ||
}, | ||
); | ||
}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.