पहले cloud_firestore_mocks के रूप में जाना जाता था। Cloud Firestore का फेक इम्प्लीमेंटेशन। Cloud Firestore का उपयोग करने वाले एप्लिकेशन के यूनिट टेस्ट के लिए इस पैकेज का उपयोग करें।
सार्वजनिक जानकारी से बना चयन मार्गदर्शन। केवल संदर्भ हेतु।
एक फर्जी Cloud Firestore का लागू करने के लिए यूनिट टेस्टिंग के लिए डिज़ाइन किया गया है। यह वास्तविक Cloud Firestore सेवा के व्यवहार की नकल करता है, जिससे डेवलपर्स को असली Firebase बैकएंड के बिना अपने ऐप के डेटा लॉजिक का परीक्षण करने में सक्षम होते हैं। यह पैकेज टेस्ट को अलग करने और विकास के दौरान स्थिर, दोहराए जा सकने वाले परिणाम सुनिश्चित करने के लिए आदर्श है। मूल रूप से cloud_firestore_mocks के नाम से जाना जाता था, यह Android, iOS, Web, macOS और Windows सहित सभी प्रमुख प्लेटफॉर्म का समर्थन करता है।
उपयोग
Firestore-निर्भर तर्क का इकाई परीक्षण
एक लाइव Firebase कनेक्शन के बिना एप्लिकेशन स्थिति का परीक्षण करना
एकीकरण परीक्षण के दौरान घटकों को अलग करना
परीक्षण में विभिन्न Firestore प्रतिक्रियाओं का अनुकरण करना
डेप्लॉयमेंट से पहले डेटा हैंडलिंग की जाँच करना
मुख्य विशेषताएँ
Cloud Firestore API के साथ पूर्ण सुसंगतता
सभी प्रमुख Flutter प्लेटफॉर्म का समर्थन करता है
परीक्षण वातावरण के लिए सरल सेटअप
मॉक्ड दस्तावेज़ और संग्रह संचालन
Firebase प्रोजेक्ट कॉन्फ़िगरेशन की आवश्यकता नहीं है
इनके लिए उपयुक्त
Flutter विकासकर्ता इकाई परीक्षण लिख रहे हैं
परीक्षण विश्वसनीयता को प्राथमिकता देने वाली टीमें
जटिल Firestore इंटरैक्शन वाले प्रोजेक्ट
परीक्षण में वास्तविक Firebase लागत से बचने वाले विकासकर्ता
निर्धारक परीक्षण परिणामों की आवश्यकता वाले ऐप्स
विचार योग्य बातें
अंत से अंत तक परीक्षण के लिए उपयुक्त नहीं
वास्तविक समय सिंक व्यवहार की प्रतिलिपि नहीं बनाता है
केवल क्लाइंट-साइड तर्क के परीक्षण के लिए सीमित
प्रारंभिक डेटा की हस्तमैथुन सेटअप की आवश्यकता है
वास्तविक Firestore के सभी किनारे के मामलों को दर्शाने में असमर्थ हो सकता है
मूल README
संबंधित सोर्स रिपॉज़िटरी के मूल README का स्नैपशॉट। नवीनतम सामग्री के लिए रिपॉज़िटरी देखें।
Fake Cloud Firestore
Fakes to write unit tests for Cloud Firestore. Instantiate a
FakeFirebaseFirestore, then pass it around your project as if it were a
FirebaseFirestore. This Fake acts like Firestore except that will only keep
the data in memory. To help debug, you can use FakeFirebaseFirestore.dump() to see what's in the
fake database. This is useful to set up the state of your database, then check that your UI
behaves the way you expect.
This project works well with firebase_auth_mocks. If you use them together, you can even test your app with security rules. See below.
Most features automatically throw exceptions depending on the state, so you do not need to mock them. For example, calling DocumentSnapshot.get on a missing field will throw a StateError.
final firestore = FakeFirebaseFirestore();
final collection = firestore.collection('test');
final doc = collection.doc('test');
await doc.set({
'nested': {'field': 3}
});
final snapshot = await doc.get();
expect(() => snapshot.get('foo'), throwsA(isA<StateError>()));
expect(() => snapshot.get('nested.foo'), throwsA(isA<StateError>()));
Mocking exceptions
Furthermore, some methods allow mocking exceptions manually, for example to simulate network errors. You can even set conditions on the parameters using the standard Dart matchers. Here are the methods which support mocking exceptions:
DocumentReference.get, set, update, delete.
Query.get.
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>()));
You can pass the security rules that you use in production in Firestore to [FakeFirebaseFirestore]. When operating on [DocumentReference] using get, set, update, or delete, [FakeFirebaseFirestore] will then check security rules and throw exceptions if access is restricted. In the example below, we restrict users/{userId} documents to their respective owners. Before they sign in, they cannot access any document inside the users collection. Once they sign in, they have access to only their own users/[uid] document.
import'package:fake_cloud_firestore/fake_cloud_firestore.dart';
import'package:firebase_auth_mocks/firebase_auth_mocks.dart';
import'package:test/test.dart';
// https://firebase.google.com/docs/rules/rules-and-auth#leverage_user_information_in_rulesfinal authUidDescription = '''
service cloud.firestore {
match /databases/{database}/documents {
// Make sure the uid of the requesting user matches name of the user
// document. The wildcard expression {userId} makes the userId variable
// available in rules.
match /users/{userId} {
allow read, write: if request.auth != null && request.auth.uid == userId;
}
}
}''';
main() async {
test('security rules' {
final auth = MockFirebaseAuth();
final firestore = FakeFirebaseFirestore(
// Pass security rules to restrict `/users/{user}` documents.
securityRules: authUidDescription,
// Make MockFirebaseAuth inform FakeFirebaseFirestore of sign-in// changes.
authObject: auth.authForFakeFirestore);
// The user signs-in. FakeFirebaseFirestore knows about it thanks to// `authObject`.await auth.signInWithCustomToken('some token');
final uid = auth.currentUser!.uid;
// Now the user can access their user-specific document.expect(
() => firestore.doc('users/$uid').set({'name': 'abc'}), returnsNormally);
// But not anyone else's.expect(() => firestore.doc('users/abcdef').set({'name': 'abc'}),
throwsException);
});
}
Right now we only support operations on DocumentReference. Later we will implement security checks for batch requests, collections and queries. Furthermore, we do not support timestamps and durations yet. See Fake Firebase Rules for an exhaustive list of what is and is not supported.
Resetting security rules
You can also dynamically change the security rules. This is useful if you want to first set up data without any restriction, then apply security rules for the actual tests.
Dump the state of the fake firebase with FakeFirebaseFirestore.dump().
Clear the fake firebase with FakeFirebaseFirestore.clearPersistence().
Create documents and collections.
Supports Converters.
Create documents with collection.add or document.set.
Batch writes and runTransaction.
Query documents with collection.snapshots or query.get.
Queries:
Filter results with query.where. The library supports equals, isGreaterThan, isGreaterThanOrEqualTo, isLessThan,isLessThanOrEqualTo, isNull, isNotEqualTo, arrayContains, arrayContainsAny, whereIn and whereNotIn.
Supports filtering by special fields like FieldPath.documentId and __name__.
Sort results with query.orderBy.
Limit results with query.limit, limitToLast, startAfterDocument, startAt, startAtDocument, endAt, endAtDocument, endBefore, endBeforeDocument. Note: startAnd and endAt work only on exact matches.
Filter with Filter, including composite filters like Filter.and and Filter.or.
Aggregate with query.count, query.aggregate(sum(field1), average(field2)...).
ValueField:
set timestamps with FieldValue.serverTimestamp(). Optionally you can pass a Clock when instantiating FakeFirebaseFirestore to define what time gets set.
delete values with FieldValue.delete().
update numerical values with FieldValue.increment.
update arrays with FieldValue.arrayUnion and FieldValue.arrayRemove.
Mock exceptions for DocumentReference.set.
Security rules:
Initialize FakeFirebaseFirestore with custom security rules.
FakeFirebaseFirestore takes authentication state from firebase_auth_mocks into account.
Reset security rules.
DocumentReference.get, set, update and delete are protected.
Compatibility table
cloud_firestore
fake_cloud_firestore
6.7.1
4.2.0
6.2.0
4.1.0
6.0.0
4.0.0
5.0.0
3.0.0
4.17.0
2.5.0
4.4.0
2.4.0
4.0.0
2.0.0
3.4.0
1.3.0
3.0.0
1.2.1
2.2.0
1.1.0
2.1.0
1.0.2
1.0.0
0.8.4
0.16.0
0.6.0
0.14.0
0.5.0
0.13.1+1
0.4.1
0.13.0+1
0.2.5
^0.12.9+6
0.2.0
Features and bugs
Please file feature requests and bugs at the issue tracker.