{"id":15436052,"url":"https://github.com/ozanturksever/mock_cloud_firestore","last_synced_at":"2025-10-04T00:05:55.504Z","repository":{"id":56834812,"uuid":"153763921","full_name":"ozanturksever/mock_cloud_firestore","owner":"ozanturksever","description":"Mock Cloud Firestore","archived":false,"fork":false,"pushed_at":"2020-04-08T04:13:31.000Z","size":91,"stargazers_count":23,"open_issues_count":4,"forks_count":13,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-29T11:41:39.255Z","etag":null,"topics":["cloud-firestore","dart","firebase","firestore","flutter","mock","testing"],"latest_commit_sha":null,"homepage":null,"language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ozanturksever.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-19T10:09:06.000Z","updated_at":"2023-04-19T12:06:29.000Z","dependencies_parsed_at":"2022-09-09T18:00:10.970Z","dependency_job_id":null,"html_url":"https://github.com/ozanturksever/mock_cloud_firestore","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ozanturksever%2Fmock_cloud_firestore","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ozanturksever%2Fmock_cloud_firestore/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ozanturksever%2Fmock_cloud_firestore/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ozanturksever%2Fmock_cloud_firestore/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ozanturksever","download_url":"https://codeload.github.com/ozanturksever/mock_cloud_firestore/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249239253,"owners_count":21235824,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cloud-firestore","dart","firebase","firestore","flutter","mock","testing"],"created_at":"2024-10-01T18:47:56.007Z","updated_at":"2025-10-04T00:05:50.462Z","avatar_url":"https://github.com/ozanturksever.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"Mock Cloud Firestore\n\nThe best way i found to test cloud_firestore is to use `Firestore.channel.setMockMethodCallHandler`.\nBut this requires knowledge of firebase protocol. This implementation tries to provide easier way.\n\n## First define the firestore data as json\n\n```dart\n  String source = r\"\"\"\n{\n  \"goals\": {\n    \"1\": {\n      \"$\": \"Goal\",\n      \"id\": \"1\",\n      \"taskId\": \"1\",\n      \"projectId\": \"1\",\n      \"profileId\": \"1\",\n      \"state\": \"ASSIGNED\"\n    }\n  },\n  \"projects\": {\n    \"1\": {\n      \"id\": \"1\",\n      \"$\": \"Project\",\n      \"title\": \"test title\",\n      \"description\": \"description\",\n      \"contributors\": [\n        \"2\"\n      ],\n      \"creatorProfileId\": \"3\",\n      \"state\": \"INCOMPLETE\"\n    },\n    \"__where__\": {\n      \"id == 1\": {\n        \"1\": {\n          \"id\": \"1\",\n          \"$\": \"Project\",\n          \"title\": \"test title\",\n          \"description\": \"description\",\n          \"contributors\": [\n            \"2\"\n          ],\n          \"creatorProfileId\": \"3\",\n          \"state\": \"INCOMPLETE\"\n        }\n      }\n    }\n  },\n  \"tasks\": {\n    \"1\": {\n      \"id\": \"1\",\n      \"$\": \"Task\",\n      \"projectId\": \"123\",\n      \"description\": \"test desc\",\n      \"closeReason\": \"\",\n      \"closeReasonDescription\": \"\",\n      \"creatorProfileId\": \"123\",\n      \"assigneeProfileId\": \"123\",\n      \"state\": \"INCOMPLETE\"\n    }\n  }\n}\n\"\"\";\n\n```\n## create the mock\n\n```dart\n  MockCloudFirestore mcf = MockCloudFirestore(source);\n```\n\n## now you can\n\n```dart\nmain() {\n  test(\"get a document\", () async {\n      MockCollectionReference col = mcf.collection(\"projects\");\n      MockDocumentReference docSnapshot = col.document(\"1\");\n      MockDocumentSnapshot docSnapshot = await doc.get();\n      expect(docSnapshot.data[\"id\"], \"1\");\n      expect(docSnapshot.data[\"title\"], \"test project 1\");\n  });\n}\n```\n\n```dart\nmain() {\n  test(\"get snapshots\", () async {\n      MockCollectionReference col = mcf.collection(\"projects\");\n      Stream\u003cQuerySnapshot\u003e snapshots = col.snapshots();\n      QuerySnapshot first = await snaphosts.first;\n      expect(first, isNotNull);\n      MockDocumentSnapshot docSnap = first.documents[0];\n      expect(docSnap.data[\"id\"], \"1\");\n  });\n}\n```\n\nTo test widgets\n\n## create a backend to wrap firestore api\n```dart\nclass BackendApi {\n  CollectionGet collectionGet;\n\n  BackendApi(this.collectionGet);\n\n  Future\u003cMap\u003cString, dynamic\u003e\u003e project() async {\n    DocumentReference docRef = collectionGet(\"projects\").document(\"1\");\n    DocumentSnapshot docSnap = await docRef.get();\n    return docSnap.data;\n  }\n}\n```\n\n## remove firestore dependency from widget\n\n```dart\nclass FirebaseDepWidget extends StatelessWidget {\n  BackendApi backend;\n\n  FirebaseDepWidget(this.backend);\n\n  @override\n  Widget build(BuildContext context) {\n    return FutureBuilder(\n      future: backend.project(),\n      builder: (BuildContext context, AsyncSnapshot snapshot) {\n        if (!snapshot.hasData) {\n          return Text(\"Loading...\");\n        }\n        return Text(\"${snapshot.data[\"title\"]}\");\n      },\n    );\n  }\n}\n```\n\n## now you can mock out firestore\n\n```dart\nvoid main() {\n  MockCloudFirestore mcf = getMockCloudFirestore();\n\n  //BackendApi realBackend = BackendApi(Firestore.instance.collection);\n  BackendApi mockBackend = BackendApi(mcf.collection);\n\n  testWidgets('check task info ', (WidgetTester tester) async {\n    await tester.pumpWidget(\n      MaterialApp(\n        home: Container(\n          child: FirebaseDepWidget(mockBackend),\n        ),\n      ),\n    );\n    await tester.pump(Duration.zero); // Duration.zero is required or you get a timer exception\n    expect(find.text(\"test project 1\"), findsOneWidget);\n  });\n}\n```\n\n## using `where` on collection\nadd expected results to collection node with `__where__` key.See example on `projects` collection.\n\n### condition format\n `field name` `operator` `expected value` \n\n### condition operators (in order)\n- isEqualTo: `==`\n- isLessThen: `\u003c`\n- isLessThanOrEqualTo: `=\u003c`\n- isGreaterThen: `\u003e`\n- isGreaterThanOrEqualTo: `=\u003e`\n- arrayContains: 'array-contains'\n- isNull: `null`\n\n### about condition order\ndart does't give order guarantee to named arguments, so order is hard coded with in code. See operators for the order.\nDon't expect logical conditions like `id \u003e 1 \u0026 id \u003c 5` this is not valid because of ordering, it must be like `id \u003c 5 \u0026 id \u003e 1`\n  \n### examples\n- `id == 1`\n- `id \u003c 5 \u0026 id \u003e 1`multiple conditions concat with ` \u0026 `, spaces are required\n- `id array-contains [\"1\", \"2\"]` value should be json encoded \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fozanturksever%2Fmock_cloud_firestore","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fozanturksever%2Fmock_cloud_firestore","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fozanturksever%2Fmock_cloud_firestore/lists"}