Unit Testing with Mockito in Flutter

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Love your new setup man. Keep up the good work. If you have time, I would love to talk with you about flutter in Chat. I would love to know your favorite state management tool?

👍︎︎ 3 👤︎︎ u/AdLive8235 📅︎︎ Sep 05 2020 🗫︎ replies

Wow cool video thank you

👍︎︎ 3 👤︎︎ u/[deleted] 📅︎︎ Sep 06 2020 🗫︎ replies
Captions
so what do you guys think i got a nice little change in setup with some lighting in the background now anyways in the last video we covered unit testing but there's a whole other part of unit testing that's that's very crucial and it's called mox so basically whenever unit testing you have a small snippet of code and you shouldn't really rely on anything outside of that snippet of code and that's where mocks come in because most code relies on something else so let's get into it [Music] all right since we have a way to illustrate things again we're definitely going to make use of it so let's pretend we have a function called from database now we have a bunch of lines of code that called the database so if you're trying to unit test this whole function this is the whole scope of that unit test nothing outside of this function should affect the test and that's where mocks come in so let's say from database you obviously need a firebase right so let's say inside this function we are testing we call a function that calls the database so during normal operation this function goes gets a call from the database but instead of going there we want it to get cut off and go to our mock database now this mock database will override all the functions from here and you get to put in your own returns so you know that the data coming in is consistent so you set your own returns and that's pretty much all there is to mocking so let's show off how you make it work with firebase in a to-do app alright so this is the app we're going to be testing we'll be testing only this auth file and only three functions so we're going to test the create account function the sign in function and the sign out function now just to show you that it all works with firebase we can log in and here we go now we can add a to-do just like that and we can restart and it'll retrieve everything from firebase so it works with firebase and we pass in our firebase authentication here and that's going to be the thing that we need to mock now before we start on the actual unit test we need to really understand what's going on here so we get streamed a user from this auth state changes class this is provided by firebase and returns a stream anytime your authentication state changes and we're passing it back using this user then we have three functions that we can act upon we have create account once you pass an email password and then you call create user with email and password and firebase creates that for you if we have a problem we catch a message then same thing with sign in we pass email password we have sign in with email and password provided by firebase and now sign out exactly the same thing we wait sign up from firebase and that's it so some key things to think about from these functions this create user with email and password doesn't directly affect the user state that we get returned from the auth class this does something to firebase and then firebase sees the reaction and returns in the off state changes all right so hopefully everybody understands this code let's get into the actual unit test so we actually won't be using this emulator anymore because you don't really need to for unit tests but i'll keep it up just to fill out the window nicely then inside here we have our main and if you guys remember there's a setup they can call and also a tear down so these get called before and after every test so set up you can write some functions in here that executes before every test and then tear down same thing but after every test then checking back to this class what was the one thing i said we were going to need this firebase auth in order to do anything we need this authentication but for us we're going to be mocking it so it's not calling the actual database in order to make your life a lot simpler with mocks we need to add this package called moquito this is basically necessary in order to be able to do mocking or else you're going to have a pretty tough time so like i said the thing we need to mock is the authentication class so we will create a mock auth class the way you mark it is you need to extend that class with with a mock you'll see we have a library imported the mokito library and then it implements our firebase auth class so basically this says we're mocking our firebase auth class and this is the class that will contain all the mocks so once we have our class defined we can create an instance of it and now we will be able to use this for all our tests so our first test we're going to call emit occurs so we're going to test that this auth state changes actually emits and we get a user from it we do that by overriding the function up here so auth state changes you just type it out i'll send you this whole thing and what we'll need to use is a string dot from iterable now inside here we're going to iterate over the type of objects that you use so you saw this should return a user so we're going to do is create a mock user as well so we'll have mock user extends mock implements user then here we can return a mock user so before we get to the actual test case i want to change this name to mock firebase auth just to make sure we're all clear that this is the firebase authentication our our class is actually called mock as well so we're not mocking this class we're mocking the firebase auth that we're passing in so now we can create our instance of our auth class as well and remember we have to pass in an authentication and that authentication is going to be mock firebase auth so there now we have our auth class that we can use for all our tests so here we want to call expect later which is for anything with futures and we're expecting the auth user class to return a stream which we can check by emits in order and inside here we'll have the mock user so there we go now if we run it well you'll actually see a problem there so we were expecting an emit of the type mock user instead we got an instance of generated stream of just user so what happened here well in flutter if you have two instances of an object let's say you do this simple check if you do mock user is equal to mock user even though they are exactly the same this will always return false this is because a separate instance of a mock user is not equal to a different instance there are packages like equatable and other ones that i'm pretty sure that actually check the properties within i tell you whether they're equal or not but since we're not using that we have another option you can create a variable for mock user and then use the same instance within this iterable and this emit check so now if we save and run everything passes great so we got the mission there now go back to this function and we need to check this first part whether create an account works so you can copy paste that and check create account so here the difference is we want to mock an actual call to database so we want to mock this create user with email and password we're not trying to override it like we did here we're trying to catch it and then return something of our own instead of the database so you you will need to do when and then inside basically do when the mock firebaseauth.createuser with email and password gets called with the fields we'll say tardis gmail dot com and password one two three four five six so when that happens then answer and we put in whatever value we wanted to answer with so actually in this case we don't really care where it answers it will just return success no matter what the answer is unless it throws an exception that's the only way it doesn't return a success and it will throw an exception if this wasn't successful basically so we mock it we don't really care what the return is and then we can do an expect where we await our auth class create account and we want if we don't pass anything that means these aren't going to get called to the specific fields that we mentioned here so i want to make sure we pass the email of todas gmail.com and password of one two three four five six now since it gets called with those exact fields it'll return with a success okay let's try to run it perfect it works now we could check the same thing except for this firebase auth exception make sure the exceptions actually work create account exception so when this function gets called instead of turning null let's actually throw a firebase auth exception and you can put your own message like you screwed up now we can actually copy this message and that should be our return for that function now we can run all three actually to check all of them pass perfect now i'm going to copy paste these and we'll do for sign in now we'll do sign in with email password pass those in sign in that should run sign in exception we do sign in with email password sign in function gets called perfect now we should be able to run those that one runs this one runs and then we could do the same thing with sign out these don't need any calls let's check how this function works so success if it's null and same thing with exception very easy sign out and there we go now we can run all of these tests and we have one two three four five six seven eight tests that all pass let's say firebase gets updated or something and the sign out function works differently our tests will not pass anymore and now we have full coverage for all of our authentication functions and let's say firebase updates they no longer throw a firebase auth exception they throw something else then one of our tests gonna fail when we run it because we only catch a firebase auth exception we don't catch the other ones so our code is more safe and you'll be able to catch problems with anything you interface with and you'll know that it's not your app that's actually broken into something else so that's it like i said at the beginning of this video you can't really have unit testing without mocking and hopefully i was able to explain it a little bit for you this code will be on github if you want to check it out and play with it you can see how you can mess up the test and stuff like that if you have any questions or anything make sure you leave in the comments make sure to like subscribe and share if you enjoyed the video and thanks for watching you
Info
Channel: Tadas Petra
Views: 9,666
Rating: undefined out of 5
Keywords: coder, amateur coder, amateur, flutter, flutter tutorial, programming, coding, flutter programming, unit testing, flutter unit testing, mocks, unit testing with mocks, mockito, unit testing mockito, flutter mockito, mocking firebase auth, mocking firebase, firebase mocks, firebase mock, unit testing with firebase, flutter mock, flutter mocks, unit testing with mockito, simple mocks, simple mockito, widget testing, integration testing, testing with mocks, simple tests
Id: 4d6hEaUVvuU
Channel Id: undefined
Length: 13min 16sec (796 seconds)
Published: Fri Sep 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.