如何使用Jasmine测试我的回调函数?

时间:2015-04-01 06:56:15

标签: jquery angularjs jasmine karma-jasmine

我有一个角度服务类: -

//从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);
      }
    );
};

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

我的单元测试是: -

// load the service's module


 beforeEach(function() {
    module('triggerTips');
  });

// instantiate service


 var userData;
  var $firebase;
  var log;
  var $rootScope;
  beforeEach(inject(function (_userData_, _$rootScope_, $log, _$firebase_, $http) {
    userData = _userData_;
    $firebase=_$firebase_;
    log = $log;
    $rootScope = _$rootScope_;
    $rootScope.clientName = 'testClient';
    $rootScope.userName = 'userDataTest';
    $rootScope.userGroupName = 'testGroup';
    $rootScope.env = 'dev';
  }));

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


  describe('initial configuration for the test user', function () {

      beforeEach(function(done){
          var config = {
                'Apache 404' : {
                  content : {
                    type : 'tip',
                    template : 'yesNo',
                    text : 'Are you searching for status code 404?',
                    yesText : 'Try our more accurate <a href="https://www.loggly.com/docs/search-query-language/#field_names" target="_blank">field search</a>',
                    attachTo : '#inputBox right',
                    yesActions : {
                      0 : {
                          type : 'replaceSubstring',
                          target : '#inputBox',
                          value : 'apache.status:404',
                          match : '404'
                        }
                    }
                  },
                  conditions : {
                    0 : {
                        type : 'valueChange',
                        target : '#inputBox',
                        textMatch : '(^|([\\s]+))404(([\\s]+)|$)',
                        preventSubmit : true
                      },
                    1 : {
                        type : 'contentPropertyLessThan',
                        propertyName : 'timesShown',
                        compareVal : 3
                      }
                  }
                }
          };

          var fireRef = new Firebase('https://luminous-inferno-1740.firebaseio.com/' + $rootScope.clientName);
          var fireSync = $firebase(fireRef);
          fireSync.$set({'config' : config});

          userData.init();
          jasmine.DEFAULT_TIMEOUT_INTERVAL = 2000;
      });

      it('should have a success log', function(done) {
          setTimeout(function() {
              expect(log.debug.logs.length > 9).toBe(true);
              expect(log.debug.logs.length).toBeGreaterThan(2);
              done();
            }, 2500);
      });

      it('should have a valid config', function () {
          expect(Object.keys(userData.getConfig()).length == 1).toBe(true);
      });
  });

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

有人可以帮我提供代码的工作示例吗?

0 个答案:

没有答案