-
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.
- Loading branch information
Showing
5 changed files
with
149 additions
and
3 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
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,26 @@ | ||
import 'package:advicer_app/application/pages/advice/bloc/advicer_bloc.dart'; | ||
import 'package:bloc_test/bloc_test.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
//'emits loading state and then error state when adviceRequest event is added' | ||
group('Adviser Bloc', () { | ||
blocTest<AdviserBloc, AdviserState>( | ||
'emits nothing when no event is added', | ||
build: () => AdviserBloc(), | ||
// nothing here since we want to have no event | ||
act: (bloc) {}, | ||
//here should be empty so no event and no state. | ||
expect: () => <AdviserState>[], | ||
); | ||
|
||
blocTest<AdviserBloc, AdviserState>( | ||
'emits nothing when no event is added', | ||
build: () => AdviserBloc(), | ||
act: (bloc) => bloc.add(AdviceRequest()), | ||
wait: const Duration(seconds: 1), | ||
expect: () => | ||
<AdviserState>[AdviserLoading(), AdviserError(error: 'Fake error')], | ||
); | ||
}); | ||
} |
85 changes: 85 additions & 0 deletions
85
test/application/pages/advice/cubit/advicer_cubit_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,85 @@ | ||
import 'package:advicer_app/application/pages/advice/cubit/advicer_cubit.dart'; | ||
import 'package:advicer_app/domain/entities/advise_entity.dart'; | ||
import 'package:advicer_app/domain/failures/failure.dart'; | ||
import 'package:advicer_app/domain/usecases/advice_usecases.dart'; | ||
import 'package:bloc_test/bloc_test.dart'; | ||
import 'package:dartz/dartz.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
class MockAdviseUseCase extends Mock implements AdviseUseCase {} | ||
|
||
void main() { | ||
group('adviserCubit', () { | ||
group('should emit ', () { | ||
final mockAdviseUseCase = MockAdviseUseCase(); | ||
// | ||
blocTest( | ||
'nothing when no event is added', | ||
build: () => AdvicerCubit(adviseUseCase: mockAdviseUseCase), | ||
expect: () => <AdviserState>[], | ||
); | ||
// | ||
blocTest( | ||
'[LoadingState , LoadedState]', | ||
build: () => AdvicerCubit(adviseUseCase: mockAdviseUseCase), | ||
setUp: () => when(() => mockAdviseUseCase.getAdvice()).thenAnswer( | ||
(_) => Future.value( | ||
const Right<Failure, AdviseEntity>( | ||
AdviseEntity(id: 1, advise: 'fake advisee'), | ||
), | ||
), | ||
), | ||
act: (cubit) => cubit.adviceRequest(), | ||
expect: () => <AdviserState>[ | ||
AdviserLoading(), | ||
AdviserLoaded(advice: 'fake advisee') | ||
], | ||
); | ||
// | ||
group('Should emit ErrorState on adviceRequest event', () { | ||
blocTest( | ||
'and a ServerFailure', | ||
build: () => AdvicerCubit(adviseUseCase: mockAdviseUseCase), | ||
setUp: () => when(() => mockAdviseUseCase.getAdvice()).thenAnswer( | ||
(invocation) => | ||
Future.value(Left<Failure, AdviseEntity>(ServerFailure())), | ||
), | ||
act: (cubit) => cubit.adviceRequest(), | ||
expect: () => <AdviserState>[ | ||
AdviserLoading(), | ||
AdviserError(error: ServerFailure().getMessage()) | ||
], | ||
); | ||
|
||
blocTest( | ||
'and a Cache Failure', | ||
build: () => AdvicerCubit(adviseUseCase: mockAdviseUseCase), | ||
setUp: () => when(() => mockAdviseUseCase.getAdvice()).thenAnswer( | ||
(invocation) => | ||
Future.value(Left<Failure, AdviseEntity>(CacheFailure())), | ||
), | ||
act: (cubit) => cubit.adviceRequest(), | ||
expect: () => <AdviserState>[ | ||
AdviserLoading(), | ||
AdviserError(error: CacheFailure().getMessage()) | ||
], | ||
); | ||
|
||
blocTest( | ||
'and a General Failure', | ||
build: () => AdvicerCubit(adviseUseCase: mockAdviseUseCase), | ||
setUp: () => when(() => mockAdviseUseCase.getAdvice()).thenAnswer( | ||
(invocation) => | ||
Future.value(Left<Failure, AdviseEntity>(GeneralFailure())), | ||
), | ||
act: (cubit) => cubit.adviceRequest(), | ||
expect: () => <AdviserState>[ | ||
AdviserLoading(), | ||
AdviserError(error: GeneralFailure().getMessage()) | ||
], | ||
); | ||
}); | ||
}); | ||
}); | ||
} |