Testing is a fundamental part of software development, whether we follow the traditional approach of writing code first and then testing it or we adopt the more modern approach of Test-Driven Development (TDD), where we write tests before writing code. The ultimate goal of testing is to ensure that the software performs as expected. In this chapter, we’ll learn how to test the BLoCs we create using the flutter_bloc library.

The bloc_test library

The flutter_bloc library’s associated testing library bloc_test is developed by the same team. The bloc_test library removes all the boilerplate code we would have needed to write to test our BLoCs.

The bloc_test library provides two main features for us:

  • Mocking the Blocs or Cubits

  • Unit testing Bloc and Cubit functions

To understand testing better, we’ll use an example of an e-commerce project.

To be able to test our app, we need to add the bloc_test library to pubspec.yaml.

Get hands-on with 1200+ tech skills courses.