在AngularJS测试中模拟/存储$ localForage的最佳方法是什么?

时间:2014-11-19 12:31:09

标签: javascript angularjs karma-runner

我想测试一个Factory,它使用$ localForage。测试工厂内部方法的最佳方法是什么?假设Karma的配置没问题。

检查以下示例:

./测试/规格/工厂/ UX-event.js

    'use strict';

    describe('Service: uxEventFactory', function () {
      beforeEach(module('paxApp'));

      var uxEventFactory;
      beforeEach(inject(function (_uxEventFactory_) {
        uxEventFactory = _uxEventFactory_;
      }));

      it('gets an instance of uxEventFactory', function () {
        expect(uxEventFactory).toBeDefined();
      });

      it('enqueues the item in the database', function () {
        var key = uxEventFactory.enqueue('1', '2', '3', '4');
        expect($localForage.getItem(key)).toBe({
          name: '1',
          action: '2',
          label: '3',
          value: '4'
        });
      });
    });

./应用程序/脚本/工厂/ UX-event.js

    'use strict';

    /**
     * @ngdoc service
     * @name paxApp.uxEventFactory
     * @description
     * # uxEventFactory
     * Factory in the paxApp.
     */
    angular.module('paxApp')
      .factory('uxEventFactory', function ($localForage) {
        return {
          enqueue: function (name, action, label, value) {
            var uxEvent = {
              name: name,
              action: action,
              label: label,
              value: value
            };
            var key = 'uxevent-' + new Date().getTime();
            $localForage.setItem(key, uxEvent);
            return key;
          }
        };
      });

0 个答案:

没有答案
相关问题