In Node/Browserify, you need to patch require
itself to override Firebase
calls. The trio of proxyquire (Node), proxyquireify (Browserify), and proxyquire-universal (both) make this easy.
// ./mySrc.js
var Firebase = require('firebase');
var ref = new Firebase('myRefUrl');
ref.on('value', function (snapshot) {
console.log(snapshot.val());
});
// ./test.js
var proxyquire = require('proxyquire');
var MockFirebase = require('mockfirebase').MockFirebase;
var mock;
var mySrc = proxyquire('./mySrc', {
firebase: function (url) {
return (mock = new MockFirebase(url));
}
});
mock.flush();
// data is logged