mardi 25 août 2015

Creating a ref in mockfirebase that points to same data

I'm unit testing a Angular Service that makes use of Firebase.

I don't want to expose the reference to the "new Firebase(...)" object if not necessary. Therefore the process to create a ref in the Service looks like this

// --Service
var _data;
var _ref = new Firebase('mock://eg.firebaseio.com/example');
_ref.on('value', function(snapshot) { _data = snapshot.val() } );
return {
    data: _data
}

Then in the Test file, I would like to dump information into this mocked Firebase

// --Test file
var ref = new Firebase('mock://eg.firebaseio.com/example');
ref.set( {test: 'success!' });
ref.flush();

This fails, as the service does not get any data. However, if I expose the ref of the Firebase in my service and then access it in the test, I can successfully dump the data and the service reads it:

// --Changed Service
var _data;
var _ref = new Firebase('mock://eg.firebaseio.com/example');
_ref.on('value', function(snapshot) { _data = snapshot.val() } );
return {
    ref: _ref
    data: _data
}

// --Changed Test file
var ref = myService.ref();
ref.set( {test: 'success!' });
ref.flush();

But this requires that I expose data which does need to be exposed by my service, and it seems the contradict the example as given on the GitHub repo: http://ift.tt/1NRFyZm

How do I dump data in the mocked firebase without exposing the Firebase ref's in the service? I've uploaded a test project here: http://ift.tt/1Khsdeb

Aucun commentaire:

Enregistrer un commentaire