javascript - What is the best way to mock/stub $localForage in AngularJS tests? -
i want test factory , uses $localforage. best way test methods inside factory? assuming karma's config ok.
check example below:
./test/spec/factories/ux-event.js
'use strict'; describe('service: uxeventfactory', function () { beforeeach(module('paxapp')); var uxeventfactory; beforeeach(inject(function (_uxeventfactory_) { uxeventfactory = _uxeventfactory_; })); it('gets instance of uxeventfactory', function () { expect(uxeventfactory).tobedefined(); }); it('enqueues item in database', function () { var key = uxeventfactory.enqueue('1', '2', '3', '4'); expect($localforage.getitem(key)).tobe({ name: '1', action: '2', label: '3', value: '4' }); }); });
./app/scripts/factories/ux-event.js
'use strict'; /** * @ngdoc service * @name paxapp.uxeventfactory * @description * # uxeventfactory * factory in 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; } }; });
Comments
Post a Comment