Exercise: Testing ShoppingCartCubit

Test your knowledge of writing tests for BLoCs.

We'll cover the following

Problem statement

In this exercise, test the ShoppingCartCubit by doing the following:

Inside test/bloc_test.dart:

  1. Create a mock for the ShoppingCartCubit.

  2. Stub the ShoppingCartCubit, and test the stream’s initial state and that it emits state in order.

  3. Create unit tests for all the functions inside ShoppingCartCubit: add the item to the cart, remove the item from the cart, and clear the cart.

You can find the ShoppingCartCubit inside lib/cubits/shopping_cart/shopping_cart_cubit.dart.

The following functions are already defined below:

  • shoppingCart() and shoppingCart1() for the ShoppingCartCubit mock group.

  • shoppingCartItem(), emptyCart(), and cartWithAProduct() for the ShoppingCartCubit functions group.

Get hands-on with 1200+ tech skills courses.