简体   繁体   English

单元测试 firebase_auth 和 firestore

[英]Unit Testing firebase_auth and firestore

I am trying to setup flutter scripts for testing my classes that interface with my firestore database.我正在尝试设置 flutter 脚本来测试与我的 firestore 数据库接口的类。

My current struggles are:我目前的挣扎是:

  • It seems that in order to use firebase_auth, I have to have a widget tree.似乎为了使用firebase_auth,我必须有一个小部件树。 I'm new to both flutter and firebase I am slowly building up my expertise by building classes one at a time, writing tests for them and then moving to the next class.我是 flutter 和 firebase 的新手,我通过一次构建一个类,为它们编写测试然后转到下一个类来慢慢建立我的专业知识。 Requiring the widget tree means I can't test the authorization logic independent of the GUI (eg What happens if I call signOut twice in my code even though the GUI logic only lets you press the button once?).需要小部件树意味着我不能独立于 GUI 测试授权逻辑(例如,如果我在代码中调用 signOut 两次会发生什么,即使 GUI 逻辑只允许您按下按钮一次?)。
  • Mocking is insufficient.嘲讽是不够的。 In order to mock, you need to know the expected behaviour for the tests to be representative.为了模拟,您需要知道测试具有代表性的预期行为。 I've spent a lot of time trying to troubleshoot issues with futures, changing signed in states, and incorrect data formats.我花了很多时间尝试解决期货、更改登录状态和不正确的数据格式等问题。 If I had written mocks, they wouldn't have matched how the real functions actually behave.如果我编写了模拟,它们就不会与实际函数的实际行为相匹配。
  • Mocking doesn't help me as I am also learning about the security rules for the database.模拟对我没有帮助,因为我也在学习数据库的安全规则。 I've definitely got code that works with lax security rules but there's something wrong with the current security rules.我肯定有适用于宽松安全规则的代码,但当前的安全规则有问题。

Is there any way to write unit/integration tests for firebase_auth or firestore without mocking?有没有办法在不模拟的情况下为 firebase_auth 或 firestore 编写单元/集成测试? I'm even happy using a local emulator/export of my database if that's a thing that's even possible.如果这是可能的,我什至很高兴使用我的数据库的本地模拟器/导出。

Here are a number of the articles I have reviewed that are not what I am looking for:以下是我查看过的一些不是我要查找的文章:

Alright, I think I have found what I am looking for.好吧,我想我已经找到了我要找的东西。 The firebase team does a really good job showing how to test security rules in Unit testing security rules with the Firebase Emulator Suite . firebase 团队在使用 Firebase Emulator Suite 单元测试安全规则中展示了如何测试安全规则的工作非常出色。

What I did was I ran firebase init in my actual flutter project directory and this article Use the Firestore Emulator with Flutter shows how to access the emulator directly in flutter, however the command to initialize is now我所做的是在我的实际 flutter 项目目录中运行了 firebase init,这篇文章使用 Flutter 使用 Firestore 模拟器展示了如何直接在 flutter 中访问模拟器, 但是现在要初始化的命令是

FirebaseFirestore.instance.useFirestoreEmulator('localhost', 8080); FirebaseFirestore.instance.useFirestoreEmulator('localhost', 8080);

Combining these, I think it should be possible to test whether or not one is writing the correct data within the correct format into the database along with ensuring the the data that's returned matches what's intended regardless of whether or not you are using Freezed, Json Serializable, dart:convert, etc...结合这些,我认为应该可以测试是否以正确的格式将正确的数据写入数据库,同时确保返回的数据与预期的匹配,无论您是否使用 Freezed、Json Serializable ,飞镖:转换等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM