Angle testing using firebase / mockfirebase in a service

I am trying to write some unit tests for my services that use AngularFire to communicate with Firebase inside the Angular website.

I am new to AngularJS and therefore feel like I am missing something obvious, but I cannot find great examples online (at least this is not due to my limited knowledge).

I found some limited documents in MockFirebase https://github.com/katowulf/mockfirebase/tree/master/tutorials , and it showed how to mock a lot of data, so I did it.

For more examples of the mock base, I looked at the Angular fire tests at https://github.com/firebase/angularfire/tree/master/tests/unit , but that didn't seem to show me correctly.

Here is my service -

app.service('Subscription', function ($firebase, FIREBASE_URL, $q) {

var ref;
var Subcription = {
ref: function () {
  if (!ref) ref = new Firebase(FIREBASE_URL + "/subscriptions");
  return ref;
},

validateSubscription: function(userId){
  var defer = $q.defer();

  $firebase(Subcription.ref().child(userId))
    .$asObject()
    .$loaded()
    .then(function (subscription) {
      defer.resolve(subscription.valid === true);
    });

  return defer.promise;
},

recordSubscription: function(userId){
  return Subcription.ref().$set(userId, {valid: true});
}

};

 return Subcription;

});

Here is the specified file -

describe('Service: subscription', function () {

  // load the service module
  beforeEach(module('clientApp'));

  // instantiate service
  var subscription;
  var scope;
  beforeEach(inject(function (_Subscription_, $rootScope) {
    MockFirebase.override();
    subscription = _Subscription_;
    scope = $rootScope.$new();
  }));

  it('allows access when the user id is in the subscription list', function () {
    subscription.ref().push({'fakeUser': {valid: true}});
    subscription.ref().flush();

    var handler = jasmine.createSpy('success');
    subscription.validateSubscription('fakeUser').then(handler);

    scope.$digest();
    expect(handler).toHaveBeenCalledWith(true);
  });

});

It seems like the problem is that the promise will never be resolved inside $ asobject. $ is loaded because this piece of corner fire does not occur.

As a result of the test, I get the following: "The expected success of the spy caused by using [true], but it was never called."

+4
source share

All Articles