使用本地JSON模拟Firebase进行测试? [英] Using local JSON to mock Firebase for testing?

查看:119
本文介绍了使用本地JSON模拟Firebase进行测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑为其数据存储使用Firebase的应用的测试策略。尽管对端到端测试向实际后端发出请求没有问题,但能够对不需要 的应用程序逻辑进行单元测试是非常好的。

我一直在网上寻找一种方式来使用像本地JSON文件来模拟JavaScript的Firebase服务,但没有发现任何东西。有这样的事情吗?如果没有,我会写它,并发布在github上...

解决方案

更新



请参阅此要点这篇文章为详细探讨封装的TDD与使用Mock进行单元测试。
$ b

原始发布

<要正确地模拟Firebase服务将是非常困难的,因为它会模拟最复杂的数据存储。想想这个嘲笑MySQL或嘲弄MongoDB

假设您的目标是使本地环境脱机工作,一个快速解决方案可能是使用本地实例。您可以抓取 firebase-debug.js ,将其保存在本地,然后致电设置(/ * data * /)将其初始化为离线操作。



否则,最好的方法是将所有Firebase活动封装到一个类或库中。那么你可以嘲笑那个库,而不是你的Firebase事件。对于准单元测试(这些技术上不是单元测试,因为它们依赖于外部服务)库本身,可以返回到本地的脱机Firebase实例或设置开发数据库并复制您的生产数据(或某些种子数据)那里。


I'm thinking about a testing strategy for an app that uses Firebase for its datastore. While it's fine for end-to-end tests to make requests to the actual backend, it would be nice to be able to make unit tests of app logic that don't have to go out to Firebase.

I've been scouring the web for a way to use something like a local JSON file to mock the javascript Firebase service, but haven't found anything. Does something like this exist? If not, I'll write it and post it on github...

解决方案

Update

See this gist and this post for a detailed exploration of encapsulated TDD vs. using mocks for unit testing.

Original Post

It would be very difficult to properly mock a Firebase service, as it would be to mock most sophisticated data stores. Think of this in terms of mocking MySQL or mocking MongoDB.

Assuming your goal is to make the local environment work offline, a quick solution might be to use a local instance. You can grab the firebase-debug.js, save it locally, and call set(/* data */) to initialize it for offline ops.

Otherwise, your best approach would be to encapsulate all the Firebase activity to one class or library. Then you can mock that library rather than your Firebase events. And to quasi-unit test (these are technically not unit tests since they depend on an external service) the library itself, fall back to a local offline Firebase instance or set up a dev database and copy your production data (or some seed data) there.

这篇关于使用本地JSON模拟Firebase进行测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆