6

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

0

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.