From 7dc8bcffa1617969aa27a4c62453b73376706ee4 Mon Sep 17 00:00:00 2001 From: Anh Tuan Nguyen Date: Tue, 3 Jan 2023 17:10:55 +0900 Subject: [PATCH] Enable DocumentReference.set to throw exceptions (#258) Enable DocumentReference.set to throw exceptions --- CHANGELOG.md | 4 ++++ README.md | 1 + lib/src/mock_document_reference.dart | 3 +++ pubspec.yaml | 3 ++- test/fake_cloud_firestore_test.dart | 19 +++++++++++++++++++ 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c05a3c40..1ec03d2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.2.0 + +Support mocking Exceptions for `DocumentReference.set` ([PR-258](https://github.com/atn832/fake_cloud_firestore/pull/258)). + ## 2.1.0 - support `GetOptions.source` when querying data. Thank you [danReynolds](https://github.com/atn832/fake_cloud_firestore/pull/253)! diff --git a/README.md b/README.md index 2763a8ad..0b34ff72 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,7 @@ See more examples at [fake_cloud_firestore/example/test/widget_test.dart](https: - delete values with `FieldValue.delete()`. - update numerical values with `FieldValue.increment`. - update arrays with `FieldValue.arrayUnion` and `FieldValue.arrayRemove`. +- Mock exceptions for `DocumentReference.set`. ## Compatibility table diff --git a/lib/src/mock_document_reference.dart b/lib/src/mock_document_reference.dart index 38b8377e..f6dff6f4 100644 --- a/lib/src/mock_document_reference.dart +++ b/lib/src/mock_document_reference.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart'; +import 'package:mock_exceptions/mock_exceptions.dart'; import 'package:rxdart/rxdart.dart'; import 'converter.dart'; @@ -179,6 +180,8 @@ class MockDocumentReference implements DocumentReference { @override Future set(T data, [SetOptions? options]) { + maybeThrowException(this, Invocation.method(#set, [data, options])); + final merge = options?.merge ?? false; if (!merge && docsData.containsKey(_path)) { docsData[_path].clear(); diff --git a/pubspec.yaml b/pubspec.yaml index 4f7e796b..3f4098ac 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: fake_cloud_firestore description: Previously known as cloud_firestore_mocks. Fake implementation of Cloud Firestore. Use this package to unit test apps that use Cloud Firestore. -version: 2.1.0 +version: 2.2.0 homepage: https://www.wafrat.com repository: https://github.com/atn832/fake_cloud_firestore @@ -16,6 +16,7 @@ dependencies: plugin_platform_interface: ^2.0.0 quiver: ^3.0.0 rxdart: ^0.27.1 + mock_exceptions: ^0.8.2 dev_dependencies: flutter_test: diff --git a/test/fake_cloud_firestore_test.dart b/test/fake_cloud_firestore_test.dart index 630a55f7..0b200da5 100644 --- a/test/fake_cloud_firestore_test.dart +++ b/test/fake_cloud_firestore_test.dart @@ -1,6 +1,7 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:fake_cloud_firestore/fake_cloud_firestore.dart'; import 'package:flutter/services.dart'; +import 'package:mock_exceptions/mock_exceptions.dart'; import 'package:test/test.dart'; import 'document_snapshot_matcher.dart'; @@ -42,6 +43,24 @@ void main() { await doc.update({'name': 'Chris'}); expect((await doc.get()).get('name'), equals('Chris')); }); + test('DocumentReference.set throws exceptions', () async { + final instance = FakeFirebaseFirestore(); + final doc = instance.collection('users').doc(uid); + whenCalling(Invocation.method(#set, null)) + .on(doc) + .thenThrow(FirebaseException(plugin: 'firestore')); + expect(() => doc.set({'name': 'Bob'}), throwsA(isA())); + }); + test('DocumentReference.set throws exceptions on certain conditions', + () async { + final instance = FakeFirebaseFirestore(); + final doc = instance.collection('users').doc(uid); + whenCalling(Invocation.method(#set, [ + {'name': 'Bob'} + ])).on(doc).thenThrow(FirebaseException(plugin: 'firestore')); + expect(() => doc.set({'name': 'Alice'}), returnsNormally); + expect(() => doc.set({'name': 'Bob'}), throwsA(isA())); + }); test('Update fails on non-existent docs', () async { final instance = FakeFirebaseFirestore(); expect(