Skip to content

Commit

Permalink
Add support for throwing exception on Query.get()
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 authored and atn832 committed Oct 24, 2023
1 parent f11b4bf commit 83caafa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/src/mock_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:math';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:fake_cloud_firestore/src/util.dart';
import 'package:flutter/services.dart';
import 'package:mock_exceptions/mock_exceptions.dart';
import 'package:quiver/core.dart';

import 'converter.dart';
Expand Down Expand Up @@ -39,6 +40,7 @@ class MockQuery<T extends Object?> extends FakeQueryWithParent<T> {
// Collection references: parent query is null.
// Regular queries: _parentQuery and _operation are not null.
assert(_parentQuery != null && _operation != null);
maybeThrowException(this, Invocation.method(#get, [options]));
final parentQueryResult = await _parentQuery!.get(options);
final docs = _operation!(parentQueryResult.docs);
return MockQuerySnapshot<T>(docs, options?.source == Source.cache);
Expand Down
24 changes: 24 additions & 0 deletions test/mock_query_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:fake_cloud_firestore/fake_cloud_firestore.dart';
import 'package:fake_cloud_firestore/src/util.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 @@ -1654,4 +1655,27 @@ void main() {
expect(query.metadata.isFromCache, false);
});
});

group('exceptions', () {
test('get', () async {
final firestore = FakeFirebaseFirestore();
final movies = firestore.collection('movies');
final query = firestore
.collection('movies')
.where('title', isEqualTo: 'Test Movie');

await movies.add({
'title': 'Test Movie',
});

whenCalling(Invocation.method(#get, null))
.on(query)
.thenThrow(FirebaseException(plugin: 'firestore'));

expect(
() async => await query.get(GetOptions(source: Source.server)),
throwsA(isA<FirebaseException>()),
);
});
});
}

0 comments on commit 83caafa

Please sign in to comment.