Jasmine + AngularJS:我如何测试firebaseio调用

时间:2015-03-25 07:13:08

标签: angularjs unit-testing jasmine karma-jasmine

我有一个角度服务类: -

    'use strict';
angular.module('triggerTips')
  .service('userData', function ($rootScope, $http, $log, $firebase) {

this._log = {
  service : 'userData'
};

// Synchronized objects storing the user data
var config;
var userState;

// Loads the user data from firebase
this.init = function(readyCallback) {
  var log = angular.extend({}, this._log);
  log.funct = 'init';

  var fireRef = new Firebase('https://luminous-inferno-1740.firebaseio.com/' + $rootScope.clientName);
  config = $firebase(fireRef.child('config')).$asObject();
  userState = $firebase(fireRef.child('userState').child($rootScope.userName)).$asObject();

  Promise.all([config.$loaded(), userState.$loaded()]).
    then(
      function() {
        if(config == null || Object.keys(config).length < 4) {
          log.message = 'Invalid config';
          $log.error(log);
          return;
        }

        if(!userState.userProperties) {
          userState.userProperties = {};
        }

        if(!userState.contentProperties) {
          userState.contentProperties = {};
        } 

        log.message = 'User Properties: ' + JSON.stringify(userState.userProperties);
        $log.debug(log);

        log.message = 'Content Properties: ' + JSON.stringify(userState.contentProperties);
        $log.debug(log);

        log.message = 'Loaded user data from firebase';
        $log.debug(log);
        readyCallback();
      },
      function() {
        log.message = 'Unable to load user data from firebase';
        $log.error(log);
      }
    );
};
 // Returns the initial tip configuration
    this.getConfig = function() {
      return config;
    };
   });

我正在尝试使用jasmine对此服务进行单元测试: -

我的单元测试是: -

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

  // load the service's module
  beforeEach(function() {
    module('triggerTips');
  });

  // instantiate service
  var userData;
  var rootScope;
  beforeEach(inject(function (_userData_, $rootScope) {
      rootScope = $rootScope;
      userData = _userData_;
  }));

  it('should load correctly', function () {
    expect(!!userData).toBe(true);
  });

  describe('after being initialized', function () {

   beforeEach(function(done) {
      // Unable to get this working because the callback is never called
        userData.init(function() {
            done();
        });
        jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;
    });

    it('should have a valid config', function (done) {
         setTimeout(function() {
             expect(Object.keys(userData.getConfig()).length > 3);
             done();
           }, 1500);
    });
  });
});

我是新手,我收到一个错误:

错误:Firebase.child失败:第一个参数是无效路径:&#34; undefiend&#34;。

有人可以通过一些解释帮我提供我的代码的工作示例吗?

0 个答案:

没有答案