So, what is Mockito and why do we need to use them while testing?
๐๐ป
Mockito is a package that lets you create Mock Dependencies while testing your units.
It helps in testing the functionality of a class in isolation. It does its processes uses dummy input rather than connecting to an external db or file server for actual data.
So how do we do Mocking in Flutter ?
We use the package: mockito
Add it to dev_dependencies in pubspec.yaml along with the latest version of build_runner.
Note: If you are facing any errors while adding the packages, feel free to try any older versions
We create a new file in the test folder and name it cats.dart
( I am following the example from the official docs of mockito)
Add the following code:
You will get errors for the import and the declaration statement after you paste the code.
Thatโs alright. Next, go to your terminal and build the code using the command below:
This will generate the cat.mocks.dart file which will contain the MockCat class with pre-filled code.
This is the Mock class that we are gonna use for the entire test cycle.
Now you can go ahead and write your tests using this instance object.
But, there are two concepts you need to know before writing tests. They are :
1. Verify
2. Stubbing
Verify is used to check whether a particular method was called on a mock object using the given parameters.
Letโs see an example:
The test will pass if the method mentioned inside verify was called earlier.
But note that the method should have the same parameter list, or else the test will fail.
Now, lets see what is Stubbing.
Check out the code below:
The method soundWithParam will return the same string which we pass as the parameter right?
What if you wanna change its behaviour while testing?
๐๐ป
What if you wanna make the method return โFlutterโ for any input string ?
We perform Stubbing.
Check the code below:
This alters the return statement when you call the method soundWithParam with any parameter.
So every time you call the method with any string as a parameter, it will return โFlutterโ as output.
Well this is the magic of Mockito. But this is not all that this package is capable of, to know more about the package, check out its official documentation:
I know this was just a basic overview of the package. I will try to write an article on this exploring all the possible functionalities of the package and share it soon.
If you liked this thread, donโt forget to like and retweet it, so that it reaches more amazing devs like you!
Stay connected with me as I share my learnings with Dart and Flutter ๐
Donโt forget to hit the notify button so that you get my contents right in your inbox!
Every time your UI refreshes (eg: using a setState), the future method is run again, which can turn out to be very expensive!
Sometimes this could be helpful, but most of the times itโs a curse for the application.
So how can you fix it ?
๐๐ป
We need to set the future in such a way that its run only once, and will never run again until you re-open the screen or you explicitly define a Swipe-down-to-refresh feature.
So, what is that one thing in Flutter, which will run only once in the entire life-cycle of your screen