anandthakker anandthakker - 3 years ago 78
Javascript Question

Using local JSON to mock Firebase for testing?

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...

Answer Source


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download