local storage - How do I unit test localStorage being undefined with Mocha/Sinon/Chai -


i have 2 simple methods abstract reading , writing localstorage:

_readlocalstorage: function(key) {     if (window.localstorage && window.localstorage.getitem(key)) {         return json.parse(window.localstorage.getitem(key));     } else {         throw new error('could not read localstorage');     } },  _writelocalstorage: function(key, data) {     try {         window.localstorage.setitem(key, json.stringify(data));     } catch (e) {         throw new error('could not write localstorage');     } }, 

obviously, stubbing window.localstorage.getitem/setitem simple. case localstorage undefined?

i've tried caching/unhinging window.localstorage (the second assertion):

describe('#_readlocalstorage', function() {     it('should read localstorage', function() {         // set         var stub1 = sinon.stub(window.localstorage, 'getitem')         .returns('{"foo": "bar"}');          // run unit         var result = service._readlocalstorage('foo');          // verify expectations         expect(result)         .to.eql({foo: 'bar'});          // tear down         stub1.restore();     });      it('should throw error if localstorage undefined', function() {         // set         var cachedlocalstorage = window.localstorage;         window.localstorage = undefined;          // run unit/verify expectations         expect(service._readlocalstorage('foo'))         .to.throw(new error('could not write localstorage'));          // tear down         window.localstorage = cachedlocalstorage;     }); }); 

this not work however. mocha/chai seem not catch thrown error.

i've looked around bit can't find way handle this.

your expect should

expect(service._readlocalstorage.bind(service, 'foo'))     .to.throw(new error('could not write localstorage')); 

the way have code calls service._readlocalstorage('foo') before expect called. raises exception expect cannot handle. expect needs able deal exceptions function expect itself call. using service._readlocalstorage.bind(service, 'foo') creates new function when called without arguments (as expect does) equivalent calling service._readlocalstorage('foo').

there's problem test: cleanup code never execute. assertion libraries report problems raising javascript exceptions. code follows failed exception won't run unless exception specially handled. do:

it('should throw error if localstorage undefined', function() {     // set     var cachedlocalstorage = window.localstorage;     window.localstorage = undefined;      // run unit/verify expectations     try {         expect(...)...;         expect(...)...;         ...     }     {         // tear down         window.localstorage = cachedlocalstorage;     } }); 

for more complex cases, should use before, beforeeach, after, aftereach setup , teardown.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -