Skip to content

Commit

Permalink
Enable DocumentReference.set to throw exceptions (#258)
Browse files Browse the repository at this point in the history
Enable DocumentReference.set to throw exceptions
  • Loading branch information
atn832 authored Jan 3, 2023
1 parent 56ab931 commit 7dc8bcf
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)!
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions lib/src/mock_document_reference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -179,6 +180,8 @@ class MockDocumentReference<T extends Object?> implements DocumentReference<T> {

@override
Future<void> 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();
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions test/fake_cloud_firestore_test.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<FirebaseException>()));
});
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<FirebaseException>()));
});
test('Update fails on non-existent docs', () async {
final instance = FakeFirebaseFirestore();
expect(
Expand Down

0 comments on commit 7dc8bcf

Please sign in to comment.