60 Days of Flutter :Building a Messenger : Day 20–21 : Unit Testing a Bloc in Flutter

Aditya Gurjar
5 min readSep 2, 2019

--

Photo by Loudge on Unsplash

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?

Posts In This Series

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!

--

--

Aditya Gurjar
Aditya Gurjar

Written by Aditya Gurjar

Mobile Engineer. Writing Mostly about Mobile Dev, Mobile DevOps, and a lil bit of life.

Responses (1)