-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Throw exceptions for testing purposes #93
Comments
This would be very useful if we could somehow simulate Network and other exceptions to better structure the tests. I'm not sure whether considering the package is a Not sure with so many nested modules in Firestore (e.g. @atn832 would love some insights on this. If this is indeed possible, I would be happy to try raise a PR. |
It's a great idea! Regarding the scope, it's indeed a bit fuzzy. If we build this, we could think of it as the fake behaving like the real Firestore for failures as well. Since we don't use Mockito's Mocks (the fakes technically still inherit Mock but I'll remove that soon), I wonder how we could implement the feature cleanly though. I'm also not familiar with the Firestore behaviors on failure. What are the most common cases where calls fail? Which cases would you like the library to be able to throw? |
Yes, currently we use Few common areas I could think of:
Another issue here is that currently Firestore's exhaustive Exception handling documentation doesn't exist yet and a big revamp is in progress there: firebase/flutterfire#2582 |
Is there any workaround for throwing exceptions right now? |
I had the same necessity. The In my opinion, we could simply have a field in the fake Firestore that receives a FirebaseException (for the I would be up for implementing this for the DocumentReference's |
We used that approach in firebase_auth_mocks, but it quickly became quite verbose. See:
Because of this, I was wary of going down the same path for fake_cloud_firestore, which is much larger project. So I took a look at how Mockito does it, and went ahead and built and published a lightweight Mockito lookalike (https://pub.dev/packages/mock_exceptions). It allowed me to much more cleanly support throwing exceptions in fake_cloud_firestore on any kinds of conditions using the standard matchers. I started with |
With fake_cloud_firestore 2.2.0, it is now possible to mock exceptions on fake_cloud_firestore/test/fake_cloud_firestore_test.dart Lines 46 to 63 in 7dc8bcf
Since supporting all the methods by myself would be unfeasible, I'll write down a little guide on how to support an exception for a method and encourage you to contribute PRs. If you are unable to contribute a PR, feel free to file a ticket for the specific exception and method you'd like to use so that we can keep track of it. First, figure out if the exception you're working with should be faked or mocked. For example, throwing an exception when calling If it should be faked, you can write something similar to how it's been done. Don't forget to include unit tests in your PR. If it should be mocked, make something similar to PR-258:
Thank you all in advance, and thanks for your patience thus far! |
Thank you so much @atn832 ! So if I understood this correctly: Exceptions that go beyond its internal state, such as exceptions related to security rules and service availability will need to be mocked. I'll contribute soon to the other methods if no one gets there before. Cheers! |
After writing over 200 commits over the past week (cel-dart is the main engine for security rules), I just released a big update for Fake Cloud Firestore regarding security rules. You can now initialize
Then So now you may want to mock exceptions only to simulate service unavailability or quota errors. |
@atn832 That's really awesome, I'll give it a go. Thanks a lot. |
@atn832 whenCalling doesn't seem to be defined... 🤔 |
@wllslmn import 'package:mock_exceptions/mock_exceptions.dart'; If you are using Visual Studio Code, it will automatically suggest that you add this import. See fake_cloud_firestore's dependencies: fake_cloud_firestore/pubspec.yaml Line 19 in d89417b
|
@atn832 thank you! |
any idea what's wrong with this code? I'm trying to mock fail on update
getting this output
|
@rajjeet Looks like you set up whenCalling with a Set ( fake_cloud_firestore/test/fake_cloud_firestore_test.dart Lines 58 to 62 in 7dc8bcf
|
@atn832 Thanks for catching that! |
CollectionReference.add() isn't supported yet? |
can anyone give me a hint with this please ( FakeFirebaseFirestore is the type of fakeFirestore ) const mail = AnswerMailModel.empty();
Expected: throws <Instance of 'FirebaseException'>
|
@kazerdira The way you set up your test was:
As you can see above, the parameters don't match. So no exception will be thrown. If you want to throw an exception next time someone calls whenCalling(Invocation.method(#add, null))
.on(collection).thenThrow(FirebaseException(plugin: 'firestore')); If you're curious about to use Finally since this feature is essentially built, I'll close this thread. Otherwise all of the previous participants will keep getting notified for no reason. Whoever has any issues can create a new ticket. |
HI @atn832, My setup is, I have a Flutter widget, where there is an "add" button. When I click on the button it calls a class method which simply executes the code below, and there is a viewing area which listens to Future<void> add(LabelEntry entry) async {
await firestore.collection('Labels').add(entry.toJson());
} The Now, my unit-test looks something like this - testWidgets('failure', (widgetTester) async {
final firestore = FakeFirebaseFirestore();
final collectionRef = firestore.collection('Labels');
whenCalling(Invocation.method(#add, null))
.on(collectionRef)
.thenThrow(FirebaseException(plugin: 'firestore'));
// pump the widget in widgetTester
await widgetTester.tap(find.text('add'));
await widgetTester.pump();
// --> this fails, since the click on the 'add' button adds this successfully
expect(find.byType(ListTile), findsNothing);
}); Would highly appreciate any help or hints. Thanks. |
It would be a very useful feature, regarding tests for exception handling, when you could throw custom exceptions on method calls (just like using when(...).thenThrow(...) in the mockito package).
The text was updated successfully, but these errors were encountered: