Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dinamic/0c6f24d733d9caceca97 to your computer and use it in GitHub Desktop.
Save dinamic/0c6f24d733d9caceca97 to your computer and use it in GitHub Desktop.

Revisions

  1. @jouni-kantola jouni-kantola created this gist Feb 2, 2014.
    41 changes: 41 additions & 0 deletions stub-properties-and-methods-sinon.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    define(['can', 'localCache'], function(can, localCache) {
    'use strict';

    describe('storeLocal()', function() {
    var sandbox;
    beforeEach(function() {
    // create sandbox environment for mocking about
    sandbox = sinon.sandbox.create();
    });

    afterEach(function() {
    // restore the environment as it was before
    sandbox.restore();
    });

    it('should cache in localStorage', function() {
    // fake localStorage
    var store = {},
    localStorageKey,
    theOneRing = {
    my: 'precious'
    },
    expected = JSON.stringify(theOneRing);

    // stub property for feature detection to use localStorage
    sandbox.stub(can.use, 'localStorage', true);

    // stub localStorage's setItem and replace with fake
    sandbox.stub(window.localStorage, 'setItem', function(key, value) {
    store[key] = value;
    localStorageKey = key;
    });

    // call local cache handler
    localCache.storeLocal(theOneRing);

    // assert
    store[localStorageKey].should.equal(expected);
    });
    });
    });