javascript - Jasmine and angular mocks : mocking a service that handles local storage -


i have 1 service called wd$cache, wrapper localstorage.setitem , get.item.

now i'm trying test controller uses service achieve result. main problem have if statement gets triggered if have localstorage set driving me nuts! (we doing tdd here)

service

(function () {  angular     .module('hub')     .controller('promotionnotificationctrl', promotionnotificationctrl);  promotionnotificationctrl.$inject = [     'hub$promotions',     'hub$client',     'wd$cache' ];  function promotionnotificationctrl(     hub$promotions,     hub$client,     wd$cache) {      var vm = this;     activate();      //////////      function activate () {          hub$promotions.get(hub$client.brand, hub$client.subbrand).success(function (data) {              if (!wd$cache.get('hub$notification')) {                  wd$cache.add('before', 123);             } else {                  wd$cache.add('after', 321);             }         });     } } })();  

test

describe('the promotion notification controller', function () {  var controller,     hub$client,     $httpbackend,     wd$cache,     mockdata = [{          "foo": "bar"     },     {         "faa": "boo"     }];   beforeeach(module('hub')); beforeeach(module('wired.core'));  beforeeach(module(function ($provide) {      hub$client = {          brand: 'bw',         subbrand: 'plus'     };      wd$cache = {          add: function () {          },          get: function () {          }     };      $provide.value('hub$client', hub$client);     $provide.value('wd$cache', wd$cache);     spyon(wd$cache, 'add'); }));  beforeeach(inject(function ($controller, _$httpbackend_, _hub$promotions_) {      controller = $controller('promotionnotificationctrl');     $httpbackend = _$httpbackend_;     hub$promotions = _hub$promotions_;      // request     $httpbackend.expectget("/umbraco/api/promotions/get/?brand=bw&lang=en&subbrand=plus").respond(200, mockdata);     $httpbackend.flush(); }));  it('should attempt add cache "before" key if no previous "hub$notification" cache found', function () {       expect(wd$cache.add).tohavebeencalledwith('before', 123); //working })  it('should attempt add cache "after" key if previous "hub$notification" cache found', function () {      localstorage.setitem('hub$notification');     wd$cache.add('hub$notification');      expect(wd$cache.add).tohavebeencalledwith('after', 123); // not working     // cant through if statement }) }); 

basically can never 'test cases' after beforeeach block, whatever do. i've tried everything, since mocking use actual storage.

any ideas?

you can provide mock implementation filled data:

var cache = {};      beforeeach(module(function ($provide) {     // ...     wd$cache = {         add: function (key, value) {             cache[key] = value;         },         get: function (key) {             return cache[key];         }     };      // add initial data here or in individual tests, e.g.       // ... })); 

to set cache specific testcase can use cache field this:

cache['hub$notification'] = 'whatever value makes sense here'; 

of course can in beforeeach.

currently trying this:

wd$cache.add('hub$notification'); expect(wd$cache.add).tohavebeencalledwith('after', 123); 

this problematic 2 reasons:

  • you not updating cache because spying on add method without .andcallthrough(). should fix (add .andcallthrough() after spy creation) otherwise updates controller not affect cache.

  • the spy records call instead. don't want setup code because makes subsequent assertions more complicated.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -