Home > Software design >  How to do Unit Testing on bloc using blocTest?
How to do Unit Testing on bloc using blocTest?

Time:10-13

I have a bloc that works to get cartItem from orderItem by id and i want to test the bloc, I've read some bloc testing from flutter_bloc but I still don't know how to implement it on my bloc. I want to do a test using dummy data that I made. I want it to get the dummy list when CartItemCheck event is emitted and remove the list when CartItemUncheck is emitted. here is my bloc code:

import...

abstract class CartItemEvent {
  const CartItemEvent();
}

class CartItemCheck extends CartItemEvent {
  final CartItem? item;

  CartItemCheck({this.item});

  List<Object> get props => [item!];

  @override
  String toString() => 'CartItemChecked { item: $item }';
}

class CartItemUncheck extends CartItemEvent {
  final CartItem? item;
  CartItemUncheck({this.item});

  List<Object> get props => [item!];

  @override
  String toString() => 'CartItemUnchecked { item: $item }';
}

class CartItemUpdate extends CartItemEvent {
  final String? itemId;
  final String? type;

  CartItemUpdate({this.itemId, this.type});

  List<Object> get props => [itemId!, type!];

  @override
  String toString() => 'CartItemUpdated { item: $itemId }';
}


class CartItemBloc extends Bloc<CartItemEvent, List<CartItem>> {
  final List<CartItem>? initialItems;

  CartItemBloc(this.initialItems) : super(initialItems!);

  @override
  Stream<List<CartItem>> mapEventToState(CartItemEvent event) async* {
    if (event is CartItemCheck) {
      List<CartItem> newItems = state..add(event.item!);
      yield newItems.toList();
    }
    else if (event is CartItemUncheck) {
      List<CartItem> newItems = state.where((item) => item.itemId != event.item!.itemId).toList();
      yield newItems.toList();
    } else if (event is CartItemUpdate) {
      CartItem oldItem = state.where((item) => item.itemId == event.itemId).first;
      List<CartItem> cartItems = state;
      if (event.type == 'increment') {
        CartItem updatedItem = oldItem.copyWith(qty: oldItem.qty   1);
        cartItems[cartItems.indexWhere((item) => item.itemId == event.itemId)] = updatedItem;
        yield cartItems.toList();
      } else {
        CartItem updatedItem = oldItem.copyWith(qty: oldItem.qty - 1);
        cartItems[cartItems.indexWhere((item) => item.itemId == event.itemId)] = updatedItem;
        yield cartItems.toList();
      }
    }
  }
}

and here is my current test code:

import 'package:bloc_test/bloc_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pikpo_screen/blocs/cart_item_bloc.dart';
import 'package:pikpo_screen/model/cart_item.dart';
import 'package:pikpo_screen/util/order_helper.dart';

main(){
  CartItemBloc? cartItemBloc;
  List<CartItem> expectedResult = [CartItem(itemId: "1"), CartItem(price: 35), CartItem(qty: 2)];
  List<CartItem> initialItem = [];
  setUp(() {
    cartItemBloc = CartItemBloc(initialItem);
  });

  test('initial state is []', () {
    expect(cartItemBloc!.state, []);
  });
  blocTest('description',
      build: () => cartItemBloc,
    act: (bloc) => bloc.add(CartItemCheck()),
    expect: () => [1]
  );
}

Thank you in advance for helping me

CodePudding user response:

In your bloc, you emit a list of CardItem, and the bloc test expects a list of states. Therefore, you have a list in a list in the expect function.

void main() {
  late CartItemBloc cartItemBloc;

  const expectedResult = [
    CartItem(itemId: '1'),
    CartItem(price: 35),
    CartItem(qty: 2)
  ];

  final initialItem = <CartItem>[];

  setUp(() {
    cartItemBloc = CartItemBloc(initialItem);
  });

  blocTest<CartItemBloc, List<CartItem>>(
    'description',
    build: () => cartItemBloc,
    act: (bloc) => bloc.add(const CartItemCheck(item: CartItem())),
    expect: () => [
      [
        const CartItem(),
      ]
    ],
  );
}
  • Related