Unit Testing with Mockito in Flutter

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ก.ย. 2020
  • Unit Testing with Mockito in Flutter
    A couple videos ago we gave a short intro to Unit Testing. However it was missing a really big part! You can't do much Unit Testing without being able to Mock! In this example we will mock firebase auth, which enables us to run tests for all the login functions.
    Widget Testing: • Widget Testing with Fl...
    Integration Testing: • Integration Testing wi...
    Link to the Code: github.com/tadaspetra/todoapp
    Connect With Me
    ✦ My Website: [tadaspetra.com/](tadaspetra.com/#/)
    ✦ Live Coding: [ / tadaspetra ]( / tadaspetra )
    ✦ Twitter: [ / tadaspetra ]( / tadaspetra )
    ✦ Chat on Discord: [ / discord ]( / discord )
    ✦ Instagram: [ / tadaspetra ]( / tadaspetra )
    I also do lots of Investing
    ✦ FREE stock: [join.robinhood.com/tadasp2](join.robinhood.com/tadasp2)
    My Top 5 Books:
    ✦ Brave New World - Aldous Huxley: [amzn.to/2JyaK96](amzn.to/2JyaK96)
    ✦ 1984 - George Orwell: [amzn.to/3lmLzUk](amzn.to/3lmLzUk)
    ✦ The Millionaire Fastlane - MJ Demarco: [amzn.to/2ViGthn](amzn.to/2ViGthn)
    ✦ The 4-Hour Workweek - Timothy Ferriss: [amzn.to/3mnypYD](amzn.to/3mnypYD)
    ✦ Homo Deus - Yuval Noah Harari: [amzn.to/2VlFRYe](amzn.to/2VlFRYe)
    TH-cam Gear
    ✦ MacBook Pro 16 inch: [amzn.to/3qe4MeF](amzn.to/3qe4MeF)
    ✦ MX Master 3 Mouse: [amzn.to/2VlkZQS](amzn.to/2VlkZQS)
    ✦ Canon Rebel SL3: [amzn.to/2ViIfPv](amzn.to/2ViIfPv)
    ✦ Hiearcool USB Hub: [amzn.to/3llsIsP](amzn.to/3llsIsP)
    ✦ iPad Pro 12.9 (3rd Generation): [amzn.to/36kaVy0](amzn.to/36kaVy0)
    ✦ Apple Pencil (2nd Generation): [amzn.to/2JoHy4y](amzn.to/2JoHy4y)
    ✦ Yeti Microphone: [amzn.to/37oRNOq](amzn.to/37oRNOq)
    ✦ Rode Boom Arm: [amzn.to/3lnEKCb](amzn.to/3lnEKCb)
    ✦ Blue Radius II Shock Mount: [amzn.to/2JoIfea](amzn.to/2JoIfea)
    ✦ Blue RPop Filter: [amzn.to/39rQSiZ](amzn.to/39rQSiZ)
    ✦ Sony WH1000XM3: [amzn.to/3moWgHh](amzn.to/3moWgHh)
    Thank you for watching!
    #unittesting #fluttertesting #testing

ความคิดเห็น • 51

  • @abraiyan7984
    @abraiyan7984 3 ปีที่แล้ว +6

    This video is a gem. This guy is teaching some valuable skills that's much more important than UI. People are just watching tutorials of beautiful UI. That's why they got more views without even talking and he got less views. His explanation style is very useful. I am sure you are not doing this just for views. Thanks for teaching us what you know. You are awesome.

    • @tadaspetra
      @tadaspetra  3 ปีที่แล้ว +3

      That means a lot! Thank you! And yes my first priority is to actually teach people about house to make good software. The views would be nice as well though 😂

  • @TemiCodes
    @TemiCodes 2 ปีที่แล้ว

    Wow. Thank you Tadas! I love this video. I’ve just learnt something from it. Thank you

  • @rebarius
    @rebarius 3 ปีที่แล้ว +1

    Oh Tadas, I love your videos! Great work man!

    • @tadaspetra
      @tadaspetra  3 ปีที่แล้ว +1

      Thank you so much!

  • @uobislrt
    @uobislrt 2 ปีที่แล้ว

    Great video Tadas. Thank you for sharing. I found it succinct but also easy to understand.

    • @tadaspetra
      @tadaspetra  2 ปีที่แล้ว

      Thank you so much!

  • @franklinoladipo2343
    @franklinoladipo2343 11 หลายเดือนก่อน

    Thank you. You explained it so well.

  • @g4mrplays
    @g4mrplays 3 ปีที่แล้ว +1

    Just going through your content, it's interesting how you get stuff straight to the point and it feels like you actually learned something compared to a lot of the stuff I've seen put out since 2018. Maybe this will force flutter content creators to finally step up w/ quality over quantity lol or you will single-handedly dominate this area. On top of that, most are extremely short videos and of things people need to actually know about.

    • @tadaspetra
      @tadaspetra  3 ปีที่แล้ว +2

      Wow, that might be the best comment I have ever received! But those things you mentioned are exactly my goals for this channel. I personally don't think I can have an attention span of more then 10-15 minutes. So that's how long I will always try to make my videos unless it's a really complex topic.
      And then the second point, I think the best way for people to learn is to start a project and learn as you do it. Whenever they get to a section that they are not sure about or need a refresher, hopefully my videos will be a quick way to really get to understand the topic, where they will be able to build upon it with this new understanding.
      I really appreciate your comment, and I will always keep learning!

  • @jrheisler
    @jrheisler 3 ปีที่แล้ว +1

    Stepping up the game, nice!!!

    • @tadaspetra
      @tadaspetra  3 ปีที่แล้ว +1

      j heisler only way to go is up 😊

  • @karolisabrutis6501
    @karolisabrutis6501 3 ปีที่แล้ว +1

    YUH YUH YUH!!

  • @RobertBrunhage
    @RobertBrunhage 3 ปีที่แล้ว +6

    Awesome video! Another video tip could be showing different naming conventions for tests!
    That way it's easier to understand the context of the test without having to read through the whole test code

    • @tadaspetra
      @tadaspetra  3 ปีที่แล้ว +1

      Robert Brunhage great too! What naming conventions do you use?

    • @RobertBrunhage
      @RobertBrunhage 3 ปีที่แล้ว +2

      @@tadaspetra I am usually using Given .... When .... Then ... (dots are the explainations)
      But a lot of people skip the "Given" and just go with "When" and "Then".
      But there are a lot of good ones and haven't had the time to look in to many others hehe

    • @tadaspetra
      @tadaspetra  3 ปีที่แล้ว +1

      Robert Brunhage I'll take a look at what is recommended😊. At work we had one test per function so we used the function name for the test name so it's easy to find with a quick search. But I think what you recommended is more useful

    • @RobertBrunhage
      @RobertBrunhage 3 ปีที่แล้ว +1

      ​@@tadaspetra Yes but the problem with that is when you rename the method you also have to rename the test. Making the tests dependant on other things than the actual logic that you test, if that makes sense.
      There is also the cases where you want to test a method multiple times like you did in the video!

    • @tadaspetra
      @tadaspetra  3 ปีที่แล้ว +1

      @@RobertBrunhage Yes those are very good points. Thanks!

  • @danielolavioferreira599
    @danielolavioferreira599 3 ปีที่แล้ว +2

    Great video, will you make one on using get for unit testing, you mentioned it on a previous video

    • @tadaspetra
      @tadaspetra  3 ปีที่แล้ว

      I think so! But also I am thinking about it, and it might not be necessary? Like instead of testing the Auth class like we did here we would just test the GetXController class. And it shouldn't be much more complex

  • @fatima-cf1bj
    @fatima-cf1bj ปีที่แล้ว

    thank you so much I learned a lot from you 😍🙏

    • @tadaspetra
      @tadaspetra  ปีที่แล้ว

      Thank you for watching :)

  • @mosaed_b
    @mosaed_b 3 ปีที่แล้ว +1

    That is so nice, thank you Tadas
    Is there a way to do it using GetX package?

    • @tadaspetra
      @tadaspetra  3 ปีที่แล้ว

      Yes definitely. So any state management solution will help with unit testing because you can load your own state. And GetX I think has some test features that make it even easier

  • @longtimedeveloper6498
    @longtimedeveloper6498 2 ปีที่แล้ว

    Thank you for a splendid tutorial. What equipment or software did you use to make the hand drawings at the beginning of the video? Super nice, thank you!

  • @yulkin2002
    @yulkin2002 3 ปีที่แล้ว +2

    Thanks for posting Tadas. How would you go about testing this code if your Auth class constructor did not take in FirebaseAuth as a parameter, but had it instead declared and initialized inside the class?

    • @tadaspetra
      @tadaspetra  3 ปีที่แล้ว +1

      That’s actually a way that testing drives good architecture. When you are having it declared inside you are not following encapsulation and abstraction principles. So you can’t really make mocks if it is inside. But also it is better code design to not put it inside. This is a complex topic to explain in text haha I hope you understand

    • @yulkin2002
      @yulkin2002 3 ปีที่แล้ว +1

      @@tadaspetra thanks!

    • @tadaspetra
      @tadaspetra  3 ปีที่แล้ว

      @@yulkin2002 your welcome 😊

  • @MrZawe
    @MrZawe 3 ปีที่แล้ว +3

    Nice more of what others rarely teach

    • @tadaspetra
      @tadaspetra  3 ปีที่แล้ว

      Zawe yea I haven’t seen much about mockito and how to use it. I struggle with it still a bit

  • @riderprem
    @riderprem 3 ปีที่แล้ว

    Great work mate! can you make a video on code coverage for the same in windows

    • @tadaspetra
      @tadaspetra  3 ปีที่แล้ว +1

      I will take a look

  • @JETLAI123
    @JETLAI123 3 ปีที่แล้ว +1

    Nice setup

  • @danielmorrissey6333
    @danielmorrissey6333 3 ปีที่แล้ว

    Great video. I was not able to get Mockito to actually throw a FirebaseAuthException. In the debug state it says its the mockFirebaseAuth, and the onStateChanged test passes, but the Success string is always returned.

    • @danielmorrissey6333
      @danielmorrissey6333 3 ปีที่แล้ว

      Your code works, there was a bug in mine that I could never find.

  • @juliosena6406
    @juliosena6406 3 ปีที่แล้ว +2

    Just Amazing! Although it would be good to show the final coverage like => flutter test --coverage && genhtml coverage/lcov.info -o coverage/html

    • @tadaspetra
      @tadaspetra  3 ปีที่แล้ว +2

      Julio Seña wow I completely forgot we have coverage reports too. Thank you for this comment!!

    • @juliosena6406
      @juliosena6406 3 ปีที่แล้ว +1

      @@tadaspetra thanks for the videos. Top 1.

  • @GAMEOLOGIST
    @GAMEOLOGIST 2 ปีที่แล้ว

    Actually, what is mocking ? I didn't understand yet. Could please explain in simple word ?

  • @paanoop
    @paanoop 3 ปีที่แล้ว

    Hi Player! did I ever tell you you're cool! Love ya friend from India

  • @kirill4531
    @kirill4531 3 ปีที่แล้ว

    Basically you're testing the Mockito lib and not the actual ability to create user/sign in/ sign out.
    If the Firebase falls you'll still have all your tests green.
    If someone completely changes the auth provider to, say, some custom backend, your tests still will be green, regardless of whether that custom auth fails or not.

    • @tadaspetra
      @tadaspetra  3 ปีที่แล้ว

      correct. But thats exactly what should happen with unit testing. Unit testing tests the "unit" so it tests that the logic within the code still works. If something changes in the backend, you can run your tests, and they will still pass. So you know it is not your code that's broken
      There are 3 types of tests however, Integration can test things outside of your app, but it is still recommended to not do that. You don't need to be testing outside code, and if you are writing a custom auth, you should probably make sure you are either still giving the same inputs, or updating your code to retrieve those inputs

    • @kirill4531
      @kirill4531 3 ปีที่แล้ว

      @@tadaspetra meh
      As for me I would rather create couple of integration tests that can definitely tell me whether the app is working correctly or not.
      Instead of tons of small unit tests which won't fail even when the app is crashed.

    • @tadaspetra
      @tadaspetra  3 ปีที่แล้ว

      @@kirill4531 you can test however you want, but that wouldn’t be the “proper” way that they do it in corporations.
      With a couple integrations tests you’ll be able to test things that you are expecting throughout the app, but then if somehow one of your functions throws an error that crashes your app, you wouldn’t catch that without a unit test

    • @tomasbaran985
      @tomasbaran985 5 หลายเดือนก่อน

      ​@@tadaspetraI think what @kirill4531 is trying to say is the following: Unit testing the boundary (the outter layer/service layer) does not test/prove/verify anything but the Mockito package itself-in other words this kind of unit test is pointless. Why? Because it is not testing your auth.dart file at all since you are mocking the outputs. So, let's say you have a bug in auth.dart but this unit test will not catch it since you are mocking the results anyhow.
      What is rather recommended is testing e.g. biz logic/controllers/states/etc. When they call the outter layer (services), we mock them instead of testing them.
      The only wise way to test the boundaries (outter layers that communicate with the outside world) is via Integration testing.
      My 2 cents. I would love to hear more comments on that since I'm also learning about that.

  • @MrVipulLal
    @MrVipulLal ปีที่แล้ว +1

    Your video is out of date. The mocks are now generated automatically with @GenerateMocks.