How to unit-test an AngularJS service that uses localStorage internally

tanmay

How do I unit-test a factory that uses $window.localStorage internally using Jasmine and ngMock?

Here's something similar to what I have:

myApp.factory('myData',function ($window) {
    return { 
        message: $window.localStorage['stuff'] 
    }
});

Thanks a bunch!

andyhasit

The following works with Jasmine 2.4:

angular.module('MyModule', []).factory('myData',function ($window) {
    return { 
        message: function(){
          return $window.localStorage['stuff'] ;
        }
    }
});

describe("TestName", function() {
  beforeEach(module('MyModule'));
  var myData, store;
  beforeEach(inject(function(_myData_) {
    myData = _myData_;
    store = {};
    var localStorage = window.localStorage;
    spyOn(localStorage, 'getItem').and.callFake(function (key) {
      return store[key];
    });
    spyOn(localStorage, 'setItem').and.callFake(function (key, value) {
      return store[key] = value + '';
    });
    spyOn(localStorage, 'clear').and.callFake(function () {
      store = {};
    });
  }));

  it("feature desc", function() {
    localStorage['stuff'] = 'hello';
    expect(myData.message()).toEqual('hello');
  });
});

Note use of the _myData_ underscore trick (see docs).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to unit test a class which uses HttpURLConnection internally?

From Dev

how to mock $window to unit test AngularJS service?

From Dev

How to unit test an AngularJS service / factory

From Dev

Angularjs Unit Test for Service

From Dev

How to test method that internally creates and uses a ServerSocket

From Dev

How do I unit test a helper that uses a service?

From Dev

How to unit test an asynchronus factory which uses another factory in AngularJS?

From Dev

How to unit test an asynchronus factory which uses another factory in AngularJS?

From Dev

How to unit test $location service search() method, in AngularJS?

From Dev

Correct way to mock an AngularJS Service in a unit test?

From Dev

Unit test service that returns promise Angularjs Jasmine

From Dev

Unit test service that returns promise Angularjs Jasmine

From Dev

How to unit test $mdSidenav service?

From Dev

How to unit test angularjs form?

From Dev

AngularJS + CanvasJS: how to unit test?

From Dev

AngularJS + CanvasJS: how to unit test?

From Dev

How to unit test a function that uses Popen?

From Dev

How to unit test code that uses AutoMapper ProjectTo?

From Dev

How to unit test a function that uses Popen?

From Dev

How to unit test a library that uses JNI?

From Dev

Unit test an angular controller and service which uses a promise?

From Dev

AngularJS, Unit testing a directive that uses a service that depends on some resources

From Dev

Jasmine/AngularJS: Inject dependent service to service in unit test?

From Dev

angularjs unit test promise based service (SQlite database service)

From Java

How do I mock a service that returns promise in AngularJS Jasmine unit test?

From Dev

How to test an angularJS service with httpBackend?

From Dev

How to test a REST service that uses JWT in SoapUI?

From Dev

How to mock AngularFire 2 service in unit test?

From Dev

How to unit test Service Fabric Actor with State

Related Related

  1. 1

    How to unit test a class which uses HttpURLConnection internally?

  2. 2

    how to mock $window to unit test AngularJS service?

  3. 3

    How to unit test an AngularJS service / factory

  4. 4

    Angularjs Unit Test for Service

  5. 5

    How to test method that internally creates and uses a ServerSocket

  6. 6

    How do I unit test a helper that uses a service?

  7. 7

    How to unit test an asynchronus factory which uses another factory in AngularJS?

  8. 8

    How to unit test an asynchronus factory which uses another factory in AngularJS?

  9. 9

    How to unit test $location service search() method, in AngularJS?

  10. 10

    Correct way to mock an AngularJS Service in a unit test?

  11. 11

    Unit test service that returns promise Angularjs Jasmine

  12. 12

    Unit test service that returns promise Angularjs Jasmine

  13. 13

    How to unit test $mdSidenav service?

  14. 14

    How to unit test angularjs form?

  15. 15

    AngularJS + CanvasJS: how to unit test?

  16. 16

    AngularJS + CanvasJS: how to unit test?

  17. 17

    How to unit test a function that uses Popen?

  18. 18

    How to unit test code that uses AutoMapper ProjectTo?

  19. 19

    How to unit test a function that uses Popen?

  20. 20

    How to unit test a library that uses JNI?

  21. 21

    Unit test an angular controller and service which uses a promise?

  22. 22

    AngularJS, Unit testing a directive that uses a service that depends on some resources

  23. 23

    Jasmine/AngularJS: Inject dependent service to service in unit test?

  24. 24

    angularjs unit test promise based service (SQlite database service)

  25. 25

    How do I mock a service that returns promise in AngularJS Jasmine unit test?

  26. 26

    How to test an angularJS service with httpBackend?

  27. 27

    How to test a REST service that uses JWT in SoapUI?

  28. 28

    How to mock AngularFire 2 service in unit test?

  29. 29

    How to unit test Service Fabric Actor with State

HotTag

Archive