I have been writing unit tests like crazy for my muse Flutter app, in my own TDD-like fashion. Writing meaningful tests and watch them go from red to green is a great feeling for real. If you aren’t doing that yet, I highly recommended.
Flutter comes with an excellent testing library called — wait a minute — test. It has one of the most comprehensive set of assertion matchers I have ever seen.
- Want to test a type? Check.
- Want to test a future? Check.
- Want to test an error emitted by a stream? Check.
- Want to test if your method accidentally rings your neighbor’s door? Umm, well, you gotta do it yourself.
A lot of times you will need to create mocks to avoid side effects in your production database or APIs. Mockito is an awesome package for that. While Mockito works great for general-purpose mocking, I found cloud_firestore_mocks to be closer to the real deal in my testing.
I have used it so extensively in my own tests that I found myself wanting for more. cloud_firestore_mocks, as awesome as it is, does not yet support 100% of Firestore’s APIs. For example, it does not yet support
arrayContainsAny where query clause. Same is true for
I wanted it so much that I implemented it myself and sent a pull request to Ahn, cloud_firestore_mocks original author. The PR has been merged and the change will land on pub.dev soon. More power to open source!
If you are using Firestore with Flutter, check out cloud_firestore_mocks today and save yourself shit loads of time troubleshooting bugs later. Highly recommended.
2 thoughts on “Mocking Firestore in Flutter”
I really appreciate that you implemented the startAt and endAt functionalities. Sad but true they are still not present in the library. Do you know when it will be merged?