From d8cadf7b73109e7ea842e5797c676815ef95347e Mon Sep 17 00:00:00 2001 From: "ghafoori.z" Date: Sun, 31 Dec 2023 10:26:17 +0330 Subject: [PATCH] Add bloc and cubit testing --- .../pages/advice/bloc/advicer_bloc.dart | 3 +- pubspec.lock | 34 +++++++- pubspec.yaml | 4 +- .../pages/advice/bloc/advicer_bloc_test.dart | 26 ++++++ .../advice/cubit/advicer_cubit_test.dart | 85 +++++++++++++++++++ 5 files changed, 149 insertions(+), 3 deletions(-) create mode 100644 test/application/pages/advice/bloc/advicer_bloc_test.dart create mode 100644 test/application/pages/advice/cubit/advicer_cubit_test.dart diff --git a/lib/application/pages/advice/bloc/advicer_bloc.dart b/lib/application/pages/advice/bloc/advicer_bloc.dart index 7e5c0ee..6f4374b 100644 --- a/lib/application/pages/advice/bloc/advicer_bloc.dart +++ b/lib/application/pages/advice/bloc/advicer_bloc.dart @@ -10,7 +10,8 @@ class AdviserBloc extends Bloc { on((event, emit) async { emit(AdviserLoading()); await Future.delayed(const Duration(seconds: 1)); - emit(AdviserLoaded(advice: 'Fake advice')); + emit(AdviserError(error: 'Fake error')); + // emit(AdviserLoaded(advice: 'Fake advice')); }); } } diff --git a/pubspec.lock b/pubspec.lock index 176032a..2e92b1a 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -9,6 +9,14 @@ packages: url: "https://pub.dev" source: hosted version: "64.0.0" + alchemist: + dependency: "direct dev" + description: + name: alchemist + sha256: "2efa90870003e10bf01af52329f2ddf9ccc44cd519adb2c8aea1713db2e84760" + url: "https://pub.dev" + source: hosted + version: "0.7.0" analyzer: dependency: transitive description: @@ -41,6 +49,14 @@ packages: url: "https://pub.dev" source: hosted version: "8.1.2" + bloc_test: + dependency: "direct dev" + description: + name: bloc_test + sha256: "02f04270be5abae8df171143e61a0058a7acbce5dcac887612e89bb40cca4c33" + url: "https://pub.dev" + source: hosted + version: "9.1.5" boolean_selector: dependency: transitive description: @@ -201,6 +217,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.10.1" + diff_match_patch: + dependency: transitive + description: + name: diff_match_patch + sha256: "2efc9e6e8f449d0abe15be240e2c2a3bcd977c8d126cfd70598aee60af35c0a4" + url: "https://pub.dev" + source: hosted + version: "0.4.1" equatable: dependency: "direct main" description: @@ -395,6 +419,14 @@ packages: url: "https://pub.dev" source: hosted version: "5.4.4" + mocktail: + dependency: "direct dev" + description: + name: mocktail + sha256: f603ebd85a576e5914870b02e5839fc5d0243b867bf710651cf239a28ebb365e + url: "https://pub.dev" + source: hosted + version: "1.0.2" nested: dependency: transitive description: @@ -666,4 +698,4 @@ packages: version: "3.1.2" sdks: dart: ">=3.2.3 <4.0.0" - flutter: ">=1.16.0" + flutter: ">=3.13.0" diff --git a/pubspec.yaml b/pubspec.yaml index 0455fa6..c3e1689 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -31,7 +31,9 @@ dev_dependencies: mockito: ^5.4.4 test: ^1.24.9 build_runner: ^2.4.7 - + bloc_test: ^9.1.5 + alchemist: ^0.7.0 + mocktail: ^1.0.2 flutter: uses-material-design: true diff --git a/test/application/pages/advice/bloc/advicer_bloc_test.dart b/test/application/pages/advice/bloc/advicer_bloc_test.dart new file mode 100644 index 0000000..800682b --- /dev/null +++ b/test/application/pages/advice/bloc/advicer_bloc_test.dart @@ -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( + '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: () => [], + ); + + blocTest( + 'emits nothing when no event is added', + build: () => AdviserBloc(), + act: (bloc) => bloc.add(AdviceRequest()), + wait: const Duration(seconds: 1), + expect: () => + [AdviserLoading(), AdviserError(error: 'Fake error')], + ); + }); +} diff --git a/test/application/pages/advice/cubit/advicer_cubit_test.dart b/test/application/pages/advice/cubit/advicer_cubit_test.dart new file mode 100644 index 0000000..9fba70f --- /dev/null +++ b/test/application/pages/advice/cubit/advicer_cubit_test.dart @@ -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: () => [], + ); + // + blocTest( + '[LoadingState , LoadedState]', + build: () => AdvicerCubit(adviseUseCase: mockAdviseUseCase), + setUp: () => when(() => mockAdviseUseCase.getAdvice()).thenAnswer( + (_) => Future.value( + const Right( + AdviseEntity(id: 1, advise: 'fake advisee'), + ), + ), + ), + act: (cubit) => cubit.adviceRequest(), + expect: () => [ + 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(ServerFailure())), + ), + act: (cubit) => cubit.adviceRequest(), + expect: () => [ + AdviserLoading(), + AdviserError(error: ServerFailure().getMessage()) + ], + ); + + blocTest( + 'and a Cache Failure', + build: () => AdvicerCubit(adviseUseCase: mockAdviseUseCase), + setUp: () => when(() => mockAdviseUseCase.getAdvice()).thenAnswer( + (invocation) => + Future.value(Left(CacheFailure())), + ), + act: (cubit) => cubit.adviceRequest(), + expect: () => [ + AdviserLoading(), + AdviserError(error: CacheFailure().getMessage()) + ], + ); + + blocTest( + 'and a General Failure', + build: () => AdvicerCubit(adviseUseCase: mockAdviseUseCase), + setUp: () => when(() => mockAdviseUseCase.getAdvice()).thenAnswer( + (invocation) => + Future.value(Left(GeneralFailure())), + ), + act: (cubit) => cubit.adviceRequest(), + expect: () => [ + AdviserLoading(), + AdviserError(error: GeneralFailure().getMessage()) + ], + ); + }); + }); + }); +}