How to Unit Test your Business Logic in Flutter

แชร์
ฝัง
  • เผยแพร่เมื่อ 13 ก.ค. 2024
  • This tutorial goes over the basics of unit testing and how to determine which unit tests to write and how to write them in Flutter.
    00:00 - Intro
    00:34 - What is unit testing
    01:11 - What is a unit
    02:11 - Rules to define units
    04:40 - What do we unit test
    05:54 - Unit test anatomy
    06:40 - Unit test naming
    08:12 - Unit test structure
    09:10 - Writing unit tests
    10:11 - Write ViewModel Tests
    14:05 - Practice unit tests to write
    14:51 - Setup unit test run shortcut
    Source: github.com/FilledStacks/stack...
    Written tutorial: www.filledstacks.com/post/how...

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

  • @mithileshparmar8443
    @mithileshparmar8443 4 ปีที่แล้ว +8

    You're my knight in armour. Always there to pump up my skills for flutter.

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

      hehe! That's great to hear. I'm happy to help

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

    I've been searching for four days how to run multiple tests. Thank you! Open terminal, point to project, type "flutter test" and that's it! Thank you big time!

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

      You're very welcome. It's weird how small things are so hidden in the docs. You would expect that to be easier to find.

  • @yakupbaser6423
    @yakupbaser6423 4 ปีที่แล้ว

    really big thanks for your tutorials&libraries&efforts. Thank you!

    • @FilledStacks
      @FilledStacks  4 ปีที่แล้ว

      I appreciate that! Thank you for leaving the comment.

  • @WayneKaminsky
    @WayneKaminsky 4 ปีที่แล้ว

    Seriously Dane!!! I really appreciate your time and effort dude. You're a major help. I hope to see you around Cape Town sometime. I hope we can return the favour someday. You rock!

    • @FilledStacks
      @FilledStacks  4 ปีที่แล้ว

      Thanks Wayne! I appreciate the kind words :) One day when we move around freely again we'll have a higher chance of "bumping" into eachother. Thanks for watching and supporting man. much appreciated.

  • @mohabmagdy4155
    @mohabmagdy4155 4 ปีที่แล้ว +11

    Dude I don't know how to thank you. you will find me on every social network appreciating your work much thanks.
    I planned today to take a full testing course on udacity this video and the course will help me test my apps.
    Keep rocking 🔥🔥🔥

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

      Niiiiiice!! Testing is a pretty broad subject so I'm happy I could help shed some light on it. I'm making some additional videos after this as follow ups as well. And thank you very much for all the support. I really appreciate it.
      if you have any additional questions after the testing then please let me know and I'll work those questions into my next few videos as well.

    • @mohabmagdy4155
      @mohabmagdy4155 4 ปีที่แล้ว

      @@FilledStacks Yes, in the next episode you said that you will start testing services please include when you get data from firebase. The firebase I already abstracted as firebase service and its functions. I made implementation separate class for it. It will be great if you showed us an example of how you test Firebase service. I want to know more about how you manage to do it within your process as no need to test Firebase itself. I wonder how you test the implemented functions of Firebase service within your app. Did you test the logic only inside The viewModel as you did in This episode or you do more Things To test Firebase Service?

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

    I just wanna thank you for such great videos, this really has helped me to be a better programmer👍👍👍.

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

      You're very welcome. It makes me happy to hear that I've made such an impact. Thanks for watching!

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

    Perfect timing. I have just started learning Unit testing. Thanks.

    • @FilledStacks
      @FilledStacks  4 ปีที่แล้ว

      Awesome! It's going to be nice and code improving journey. Goodluck.

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

      Hi bro I need some help from unit testing

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

      What do you need help with?

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

      I am new in unittesting bro.how to start in unittesting any idea.and also structure of project in unittesting

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

      The same way I show in the video :) that's why the guide is there.

  • @xaxababa6616
    @xaxababa6616 4 ปีที่แล้ว

    Thanks for this! Appreciate your efforts!

    • @FilledStacks
      @FilledStacks  4 ปีที่แล้ว

      You're welcome. I appreciate your comment

  • @dc5
    @dc5 4 ปีที่แล้ว

    Keep them coming, please 😀. Thank you, Dane! 👌

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

      David! I will try my best to expand on the unit testing more. i think i'll most likely have to make a course to fully show the technique and approach to covering your code base with tests.

    • @dc5
      @dc5 4 ปีที่แล้ว

      @@FilledStacks can't wait to see it.. it's always interesting to see your take on all these stuff. Very nice job man, and as usual, excellent video!

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

    your channel should have 1000000000000000~ subscribers .

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

      haha, that's more people than there are in the world but I'll take it !!!! :) One day we'll have a bigger community, it takes time and lots of sharing

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

    I think I just found a gem, thank you very much

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

      I'm very happy to hear that :) There's a new video out for unit testing as well.

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

    I always ran away from testing but after your deep explanation everything is coming together. I wanted to learn testing for quite a long time but didn't find anything to motivate me. Now I just love it. Thanks for the effort that you put into your content. And the most awesome thing is that its for free.

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

      I have a little question which might be weird but still its in my mind so I think I should ask it.
      What happens to the testing code that we write after we compile our app? Does it get removed automatically or we have to do something manually to prevent the testing code from being compiled?

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

      That's awesome to hear! Thanks for letting me know. I'm happy you enjoy unit testing :) It's nice to work with once you get the hang of it.

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

      You don't have to do anything. Flutter only builds the code in the lib folder. Nothing else :) Test files only run on your machine when you execute the command to run it.

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

      @@FilledStacks Thanks alot.

  • @berosolo866
    @berosolo866 4 ปีที่แล้ว +2

    its been a long time my friend , we missed you
    also , i have been thinking lately about TDD and how to implement it without complicating my code and this might be the starting point

    • @FilledStacks
      @FilledStacks  4 ปีที่แล้ว

      :) aaaaawwww man ... And yes, I do a very non-strict version of TDD. I think just using these "rules" in here will help you define what to test or what to define as a unit. As you gain more unit testing experience that will most likely change but I think it's a good starting point.

  • @augustemmery-funch620
    @augustemmery-funch620 4 ปีที่แล้ว

    Great video as always! I, myself, have thought about changing to more of a TDD aproach:)

    • @FilledStacks
      @FilledStacks  4 ปีที่แล้ว

      I do a very laid back TDD approach compared to how I use to do it. It's been very beneficial so far. I'll share more about that as I figure out what I'm doing to make the process less intrusive for new comers to TDD.

  • @thiyraashdavid7784
    @thiyraashdavid7784 4 ปีที่แล้ว

    Thanks man!

    • @FilledStacks
      @FilledStacks  4 ปีที่แล้ว

      It's my pleasure to have made it.

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

    Please! add a video of How to use Authentication Service in your stacked architecture.

    • @FilledStacks
      @FilledStacks  4 ปีที่แล้ว

      Hey, I don't think that's required. It'll be literally exactly the same as I showed in the firebase series. Exactly the same, using stacked doesn't change how services are used. Service classes are used in every architecture.

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

    Hey love your videos. It really has made me realize how many mistakes and pitfalls I've made in my programs and makes me a better programmer overall. For your note on widgets not being in the ViewModel. In one of my apps I have put the streambuilder widget which listens to onauthchanges from firebase in my viewmodel (based on the log in status a different page is returned). I did this as I did not want the routing logic within my UI but it violates the rule of not having widgets in my viewmodel. Do you have any advice on how better to implement this feature?

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

      Hey, yes. You can use a StreamViewModel and do the same thing in your viewmodel then simply call the navigationService.navigateTo with your route name you want to go to.

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

    Can we get this guy a million subs!!!

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

      Haha, thank you and I appreciate it! I'd be happy to get to 100k soon :D

  • @RobertBrunhage
    @RobertBrunhage 4 ปีที่แล้ว

    Oh those snippets for creating tests etc looks sweet, what did you do to get them?

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

      Thanks Robert. I made them, i posted them to a gist. I'll be updating these snippets as I need more things during development. gist.github.com/FilledStacks/b57b77da10fdcb2d4d95a28de4a4ced4

    • @RobertBrunhage
      @RobertBrunhage 4 ปีที่แล้ว

      @@FilledStacks Awesome looking forward to more! :)

  • @francisrib5319
    @francisrib5319 4 ปีที่แล้ว

    Wow !! I'm falling in love with this last series and I'm starting to use it in my apps !! I am now confronted with error handling. How do you do to make them climbed up from Services to the View ? Are you planning to release a video on this topic or do you have some recommendations regarding this subject ? Thanks in advance

    • @FilledStacks
      @FilledStacks  4 ปีที่แล้ว

      Awesome! that's great news. I will make a video about it. I'm trying out things myself and will need to add some additional functionality into stacked to do it the way I want it to be done. currently I throw exceptions and it's handled in the viewModel. I'm removing the try/catch statements. In the specialised viewmodels we already have the onError callback which returns your exception and sets a property on the viewmodel. I want to build that into the base viewmodel as well to handle errors thrown. But I will make a video about it.

    • @francisrib5319
      @francisrib5319 4 ปีที่แล้ว

      Ok !! Nice !!
      I'm going to try your way and see how efficient it is.
      I'll be waiting your next videos for sure !! ❤

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

    As always, outstanding tutorial. Would really love to see a series on TDD! Quick couple questions: do you use widget tests in when you test your code? What is the point of setup/test_helpers as you don't use them at all? Finally, do you recommend saving globalKeys in view models, because you mentioned no UI code in the viewmodel. I will commonly store the globalKey in a viewmodel, and call .validate(), and .save() for a formKey within a function in my viewmodel.

    • @FilledStacks
      @FilledStacks  4 ปีที่แล้ว

      Thank you.
      - I don't use widget tests
      - setup/test_helpers and data will be used in the next video where we have to register services before tests and setup data for the test we'll add functions in those files that wrap all that functionality
      - No, no ui code in the ViewModel. You won't be able to unit test properly because it'll be reliant on flutter which you can't mock out.

    • @gabrielmcreynolds7947
      @gabrielmcreynolds7947 4 ปีที่แล้ว

      @@FilledStacks Thanks for your quick response! Can't wait for your next video

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

      @@FilledStacks Is there any particular reason why you don't use widget tests? I write unit tests for all my view models and services but widget tests are hard to test with a view model. Do you consider it not worth it to test out widgets?

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

      Yes, that's exactly the reason. I don't find it worth it to write. I would rather use it for full integration testing instead of testing a single widget.

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

    I've recommended your videos to my friends, but they are beginners and kinda dont know where to start. Can you post a Roadmap to watch your videos? like in what order we should watch them , that would be really helpful. I use the numbering in the Github repo for now. Also , can you add some "playlist" like feature on the website? to group all series related articles in one place. Like the firebase series or architecture series in youtube playlists.

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

      that sounds like quite a lot of work and i'm already struggling to put out 1 video every 2 weeks. We won't have time to dedicate to the website, right now. I don't recommend learning for the same of learning either. build something, if you need to know how to do just one thing then google that and learn that from my videos or the other creators. Just going through content doesn't actually work as we can see from school as well as university to some degree. They should learn what they need to know, i probably know less than 30% of flutter. I only learn the things I need to know to solve to problem at hand.

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

    I know unit tests are supposed to be independent of each other. What is your approach to writing tests for calls that depend on and adjust state for each other?

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

      I don't have an approach for that because I personally spend my time making sure that's not the case. Won't be able to give you any pointers there. i keep unit tests in units, testing only 1 unit of work / 1 state at a time.

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

    Question:
    What’s if the methods being tested in the viewmodel use dialog service or snackbar service?
    How to test this type of method?

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

      good questions, you create a mock for it using Mockito and then you can confirm it was called with the correct data and you can make it return anything you want incase you're expecting a response from it. That's in the mocks video :D

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

    Can you do a video on how to use gRPC framework on flutter ?

    • @FilledStacks
      @FilledStacks  4 ปีที่แล้ว

      I probably can not. I have no idea what gRPC is. Been hearing about it. If I ever have to use it in production then you'll get a tutorial. I only share what we use in production as a team, so far I've never used gRPC so I've got 0 knowledge of it.

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

    Hi, Please make video on MVVM / MVC / MVP Architecture, which is most commanly used in a 5 -20 minute video is possible.

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

      Hey, Stacked is based on mvvm so you can go through those and you'll see how I use it.

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

      @@FilledStacks okay, thanks

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

      @@FilledStacks , can I know if we are using MVVM pattern / stacked,
      Then where should we handle UI dailogs and routing, permission denied prompts, etc, do we need to do all in provider { View Model } by taking context in method call parameter or we should do in UI
      Thankyou

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

      @@mruduladdipalli5417 Hey, stacked is a package that implements a version of MVVM. I show all of that in my stacked videos. Interfaces are available in the viewmodels where the business logic lives, but the dialog is shown at a different place. It'll be better to watch my latest state management setup video.

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

    Anyone watching this during quarantine?

    • @FilledStacks
      @FilledStacks  4 ปีที่แล้ว +2

      not exactly watching during quarintine but I made it during quarintine :)

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

      @@FilledStacks lol

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

      @@gameplaymaster5502 :)

  • @berozgaarengineer3999
    @berozgaarengineer3999 4 ปีที่แล้ว

    W/FlutterActivity( 7241): Tried to automatically register plugins with FlutterEngine (io.flutter.embedding.engine.FlutterEngine@4d9c159) but could not find and invoke the GeneratedPluginRegistrant..
    ..
    .
    How can I solve this issue?!?

    • @FilledStacks
      @FilledStacks  4 ปีที่แล้ว

      Make sure you're not trying to unit test something that depends on flutter or a plugin

    • @berozgaarengineer3999
      @berozgaarengineer3999 4 ปีที่แล้ว

      @@FilledStacks
      Flutter showing this issue because I didn't migrate my project in
      Upgrading pre 1.12 Android projects

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

      @@berozgaarengineer3999 Aaaaaah, I see. thanks for sharing the solution

    • @reflex7196
      @reflex7196 4 ปีที่แล้ว

      @@FilledStacks I am implementing firebase authentication in flutter project but I'm getting error due to above mention issue (tried to automatically register...), I tried solution upgrading pre 1.12 Android project. But works for me!
      ..
      Please make video on upgrading pre 1.12 android project..

  • @materialapp
    @materialapp 4 ปีที่แล้ว

    How to test sqlite database without running the emulator?

    • @FilledStacks
      @FilledStacks  4 ปีที่แล้ว

      You'll need an implementation of SQLite that allows you to run an in memory version of it.

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

    Your videos are great but people get bored when you only talk & show an Article in a video for long time. I'd have loved it more if you were also doing the things you were explaining from the article/composition in realtime. That'll make the video shorter & people won't get lost like I get lost in the example part
    It's just my personal opinion

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

      Hey Hey, thanks. I prefer to talk about the actual implementation and give more details when I'm writing the code. The first part should serve as an overview. Most viewers cannot translate plain english directly into the code so me talking in normal english and showing the code could cause even more confusion because I won't stop to explain the code.
      It's easier to follow if you a path of where we're going. Just jumping into things make the tutorials harder to follow.