Flutter/Package

[Flutter] Bloc ⑤ - Bloc Test

찌김이 2022. 8. 5. 15:47
728x90
반응형

Bloc ① - 소개  https://dalgoodori.tistory.com/28

Bloc ② - Cubit 과 Bloc https://dalgoodori.tistory.com/29

Bloc ③ - Bloc Widgets https://dalgoodori.tistory.com/30

Bloc  - context.read, context.watch, context.select https://dalgoodori.tistory.com/31

Bloc ⑤ - Bloc Test

 

 

bloc_test | Dart Package

A testing library which makes it easy to test blocs. Built to be used with the bloc state management package.

pub.dev

 

pubspec.yaml

dependencies:
  bloc_test: ^9.0.3

 

lib/counter_bloc.dart

abstract class CounterEvent {}

class CounterIncrementPressed extends CounterEvent {}

class CounterDecrementPressed extends CounterEvent {}

class CounterBloc extends Bloc<CounterEvent, int> {
  CounterBloc() : super(0) {
    on<CounterIncrementPressed>((event, emit) => emit(state + 1));
    on<CounterDecrementPressed>((event, emit) => emit(state - 1));
  }
}

 

test/test.dart

void main() {

  // ① 테스트할 그룹을 만듭니다.
  group('Test Group', () {
    late CounterBloc counterBloc;
	
    // ② 테스트할 블럭은 setUp() 에서 합니다
    setUp(() {
      counterBloc = CounterBloc();
    });
    
    // ③ 블럭을 테스트 합니다.
    blocTest(
      'emits [1] when CounterIncrementPressed is added', // ④ 테스트 명칭
      build: () => counterBloc, // ⑤ 테스트할 블럭
      act: (_) => counterBloc.add(CounterIncrementPressed()), // ⑥ 테스트할 이벤트
      expect: () => [1], // ⑦ 결과값
    );
  }
  
  blocTest(
    'emits [1] when CounterIncrementPressed is added',
    build: () => CounterBloc(),
    act: (CounterBloc bloc) => bloc.add(CounterIncrementPressed()),
    expect: () => [1],
  );
}

 

 

 

 

Bloc State Management Library

Official documentation for the bloc state management library. Support for Dart, Flutter, and AngularDart. Includes examples and tutorials.

bloclibrary.dev

 

728x90
반응형

'Flutter > Package' 카테고리의 다른 글

[Flutter] photo_manager  (0) 2022.08.11
[Flutter] dio  (0) 2022.08.07
[Flutter] Bloc ④ - context.read, context.watch, context.select  (0) 2022.08.05
[Flutter] Bloc ③ - Bloc Widgets  (0) 2022.08.04
[Flutter] Bloc ② - Cubit 과 Bloc  (0) 2022.08.04