how to mock $window to unit test AngularJS service?

Arun

Here is my service:

var services = angular.module('amdotcom.services', ['ngResource']);

services.factory('homePageRes', ['$resource', '$window',
    function ($resource, $window) {
       var wdw = angular.element($window);
       return $resource('home/index', {
           height: wdw.height(),
           width: wdw.width()
       });
   }]);

services.factory('homePageLoader', ['homePageRes', '$q',
  function (homePageRes, $q) {
      return function () {
          var delay = $q.defer();
          homePageRes.get(function (homePage) {
              delay.resolve(homePage);
          }, function () {
              delay.reject('Unable to fetch home page');
          });
          return delay.promise;
      };
  }]);

Below is my test from before introducing the $window service. These tests worked fine then, but once I introduced the $window service, I'm unable to mock it.

describe('Services', function () {

    beforeEach(function () {
        module("amdotcom.services");
    });

    describe('homePageLoader', function () {
        var mockBackend, loader;

        beforeEach(inject(function ($httpBackend, homePageLoader) {
            mockBackend = $httpBackend;
            loader = homePageLoader;
        }));

        it('should return home page information', function () {
            mockBackend.expectGET('home/index?height=400&width=500').respond({ "Albums": [{ "Id": 2, "Name": "best shots" }] });

            var homePageData;

            var promise = loader();
            promise.then(function (homePg) {
                homePageData = homePg;
            });

            expect(homePageData).toBeUndefined();

            mockBackend.flush();

            expect(homePageData.Albums[0].Id).toEqual(2);
            expect(homePageData.Albums[0].Name).toEqual("best shots");
        });

        afterEach(function () {
            mockBackend.verifyNoOutstandingExpectation();
            mockBackend.verifyNoOutstandingRequest();
        });
    });
});

I am getting the error saying: TypeError: 'undefined' is not a function (evaluating 'wdw.height()')

I tried using the $provider and the spyOn methods, but none are working. Please help.

Arun

Arun

Apparently the height() and the width() functions are incorrect. I changed them to innerHeight and innerWidth properties.

services.factory('homePageRes', ['$resource', '$window',
    function ($resource, $window) {
       return $resource('home/index', {
           height: $window.innerHeight,
           width: $window.innerWidth
       });
    }]);

beforeEach(function () {
    angular.mock.module("amdotcom.services", function ($provide) {
        var myMock = {
            innerHeight: 400,
            innerWidth: 500
        };
        $provide.value('$window', myMock);
    });
});

Since my comment above was not formatted and less readable, I've added it again here.

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 mock $window.location.replace in AngularJS unit test?

From Dev

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

From Dev

How to mock AngularFire 2 service in unit test?

From Dev

How to mock AngularFire 2 service in unit test?

From Java

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

From Dev

How to test function in AngularJS service with mock data

From Dev

How to unit test an AngularJS service / factory

From Dev

Angularjs Unit Test for Service

From Dev

How do I mock the result of a promise in an AngularJS unit test?

From Dev

How do I mock the result of a promise in an AngularJS unit test?

From Dev

How to mock an event like 'keypress' or 'keydown' in an AngularJS unit test?

From Dev

Unit testing the AngularJS $window service

From Dev

How do I mock $http in AngularJS service Jasmine test?

From Dev

How to mock springSecurityService in an unit test

From Dev

How to mock this unit test in Python?

From Dev

Grails Spock Mock Injected Service in Unit Test

From Dev

mock angularjs service for unit testing using jasmine

From Dev

AngularJS override (mock) services for unit test

From Dev

angularjs unit test karma with jasmine mock dilemma

From Dev

AngularJS override (mock) services for unit test

From Dev

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

From Dev

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

From Dev

Angular Karma Unit Test: How to inject a mock service in karma configuration instead of in all test specs

From Dev

AngularJS Controller As Jasmine test with mock service with promise

From Dev

Unit test for angularjs with $window.location.pathname

From Dev

AngularJS - unit tests - using original service from mock of same service?

From Dev

How do you mock classes that are used in a service that you're trying to unit test using JUnit + Mockito

From Dev

Spring Integration Java DSL unit test - How to mock a Service Activator class or other components/endpoints?

From Dev

Unit test service that returns promise Angularjs Jasmine

Related Related

  1. 1

    How to mock $window.location.replace in AngularJS unit test?

  2. 2

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

  3. 3

    How to mock AngularFire 2 service in unit test?

  4. 4

    How to mock AngularFire 2 service in unit test?

  5. 5

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

  6. 6

    How to test function in AngularJS service with mock data

  7. 7

    How to unit test an AngularJS service / factory

  8. 8

    Angularjs Unit Test for Service

  9. 9

    How do I mock the result of a promise in an AngularJS unit test?

  10. 10

    How do I mock the result of a promise in an AngularJS unit test?

  11. 11

    How to mock an event like 'keypress' or 'keydown' in an AngularJS unit test?

  12. 12

    Unit testing the AngularJS $window service

  13. 13

    How do I mock $http in AngularJS service Jasmine test?

  14. 14

    How to mock springSecurityService in an unit test

  15. 15

    How to mock this unit test in Python?

  16. 16

    Grails Spock Mock Injected Service in Unit Test

  17. 17

    mock angularjs service for unit testing using jasmine

  18. 18

    AngularJS override (mock) services for unit test

  19. 19

    angularjs unit test karma with jasmine mock dilemma

  20. 20

    AngularJS override (mock) services for unit test

  21. 21

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

  22. 22

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

  23. 23

    Angular Karma Unit Test: How to inject a mock service in karma configuration instead of in all test specs

  24. 24

    AngularJS Controller As Jasmine test with mock service with promise

  25. 25

    Unit test for angularjs with $window.location.pathname

  26. 26

    AngularJS - unit tests - using original service from mock of same service?

  27. 27

    How do you mock classes that are used in a service that you're trying to unit test using JUnit + Mockito

  28. 28

    Spring Integration Java DSL unit test - How to mock a Service Activator class or other components/endpoints?

  29. 29

    Unit test service that returns promise Angularjs Jasmine

HotTag

Archive