60 Days of Flutter :Building a Messenger : Day 20–21 : Unit Testing a Bloc in Flutter
In my last post I wrote the Unit Tests for the Providers
using mockito. Today we’ll be continuing on and writing tests for our AuthenticationBloc
.
This is probably going to be the last post related to Unit Testing in this series, We’ve covered unit testing of widgets, providers and Blocs in different posts. I’ll be writing tests for all the code that I’ll do for this app but I’ll explain/post about it only if I feel there’s anything new to explain. So let’s get started!
Before we start, shout-out to Felix Angelov for this incredibly well put together post about Unit testing of Bloc.
What is to be tested in a Bloc?
Unit testing of Blocs is used to verify that,
For every input event, the sequence of states emitted by the Bloc is always consistent and predictable.
In simple words, in today’s tests we’ll be verifying if after every dispatch of a particular event, the sequence of states emitted by the Blocs is what we expected it to be.
Setup
Create a file, tests/blocs/AuthenticationBloc_test.dart
and start by initializing all the mock objects we need for this test.
We first declare all the mock objects and initialize them one by one in the setup()
block.
Our first test will verify that the initial state is always Unintialized
(we declared it in the AuthenticationBloc
class if you recall).
Testing for each individual Event
We had the below six events declared in our AuthenticationEvent.dart
file.
We’ll test them one by one, each event may have multiple outcomes based on the state of the app at that moment. You’ll see how, when we test our first Event AppLaunched
.
The AppLaunched
event is dispatched right at the start of the app. At that point we can have three possible scenarios.
- User is not logged in -> go to login screen.
- User is logged in and profile is complete -> go to home screen.
- User is logged in and profile is incomplete -> go to profile completion screen.
We have the tests for all these flows in three different tests. So in practice, our primary goal will be to test for all the possible states/flows that are possible whenever a particular event is dispatched.
Here we first mock the function responses according to the flow we desire, then we create an array of the states we’re expecting (in sequence) and then we use the expectLater()
method to queue these States
. Now when we use authenticationBloc.dispatch
, it dispatches a Event
, causing a change in State
and these changes in States
are compared with our expected sequence of States
.
Similarly we can also write tests for the rest of the Events.
Bloc testing might look an easy thing to do and even unnecessary to some, but it’s extremely important in making sure that the behavior of our App and it’s Business Logic is consistent with what we’re expecting.
That’s all for today folks!
I’ll be off for the next few days, so I’ll catch you guys after that in the next part where I’ll be working on building the Chat Screen using Firebase.
Hit me up in the comments below or Twitter or Linkedin or Instagram if you have any queries or suggestions. See you guys in the next one!
Code Changes
#15 Added Tests for AuthenticationBloc
How Can You Contribute?
- Open issues with suggestion of better approaches or ideas for the app.
- Connect with me on Twitter or Linkedin or Instagram.
- Star the Github repository.
- Share the series on Twitter.
- Follow me on Github.
Posts In This Series
- 60 Days Of Flutter : Building a Messenger from Scratch
- 60 Days of Flutter : Day 1 : Creating the App
- 60 Days of Flutter : Day 2 : Setting Up A CI With Flutter
- 60 Days of Flutter : Day 3–4 : Building a Chat Screen in Flutter
- 60 Days of Flutter : Day 4–5 : Widget Testing With Flutter
- 60 Days of Flutter : Day 6–7 : Implementing a Slideable Widget Using Bottomsheet in Flutter
- 60 Days of Flutter : Day 8 : Changing The Launcher Icon and Implementing GestureDetector
- 60 Days of Flutter : Day 9–10–11 : Creating Awesome Register Screen in Flutter
- 60 Days of Flutter : Day 12–14 : Understanding BLoC Pattern in Flutter
- 60 Days of Flutter : Day 15–17 : Implementing Registration Screen using ‘flutter_bloc’
- 60 Days of Flutter : Day 18–19 : Unit Testing in Flutter using ‘ mockito’
- 60 Days of Flutter : Day 20–21 : Unit Testing a Bloc in Flutter
- 60 Days of Flutter : Day 22–23 : Building a Modern Contacts Page in Flutter
- 60 Days of Flutter : Day 24–26 : Building a Animated Progress Fab and the Contacts Bloc in Flutter
- 60 Days of Flutter : Day 27–29 : Sending and Retrieving Messages from Firebase using BLOC
- 60 Days of Flutter : Day 30–32 : Firebase Chat UI using Stream and Bloc
- 60 Days of Flutter : Day 33–35 : Paginating data from Firestore using Firebase Queries
- 60 Days of Flutter : Day 36–38 : Seamlessly Upload Files to Firebase Storage
- 60 Days of Flutter : Day 39–41 : One UI Inspired Attachments Showcase Page
- 60 Days of Flutter : Day 42–45 : Creating the Home Page & Quick Peek BottomSheet for Messages
- 60 Days of Flutter : Day 45–47 : Adding Dark Mode to a Flutter App
- 60 Days of Flutter : Day 48–50 : Creating the Settings Page using Bloc
- 60 Days of Flutter : Day 51–54 : Unit Testing Firebase Providers with Mockito
- 60 Days of Flutter : Day 55–56 : Deploying Firestore Security Rules using Firebase CLI
- 60 Days of Flutter : Day 60 : Wrapping It Up
Show Your Support
Press the clap button below if you liked reading this post. The more you clap the more it motivates me to write better!