Testing
Esta página aún no está disponible en tu idioma.
Bloc was designed to be extremely easy to test. In this section, we’ll walk through how to unit test a bloc.
For the sake of simplicity, let’s write tests for the CounterBloc
we created in Core Concepts.
To recap, the CounterBloc
implementation looks like:
Before we start writing our tests we’re going to need to add a testing framework to our dependencies.
We need to add test and bloc_test to our project.
Let’s get started by creating the file for our CounterBloc
Tests, counter_bloc_test.dart
and importing the test package.
Next, we need to create our main
as well as our test group.
Let’s start by creating an instance of our CounterBloc
which will be used across all of our tests.
Now we can start writing our individual tests.
At this point we should have our first passing test! Now let’s write a more complex test using the bloc_test package.
We should be able to run the tests and see that all are passing.
That’s all there is to it, testing should be a breeze and we should feel confident when making changes and refactoring our code.
You can refer to the Weather App for an example of a fully tested application.