Unit testing the AngularJS $window service

balteo

I would like to unit test the following AngularJs service:

.factory('httpResponseInterceptor', ['$q', '$location', '$window', 'CONTEXT_PATH', function($q, $location, $window, contextPath){
     return {
         response : function (response) {
             //Will only be called for HTTP up to 300
             return response;
         },
         responseError: function (rejection) {
             if(rejection.status === 405 || rejection.status === 401) {
                 $window.location.href = contextPath + '/signin';
             }
             return $q.reject(rejection);
         }
     };
}]);

I have tried with the following suite:

describe('Controllers', function () {
    var $scope, ctrl;
    beforeEach(module('curriculumModule'));
    beforeEach(module('curriculumControllerModule'));
    beforeEach(module('curriculumServiceModule'));
    beforeEach(module(function($provide) {
       $provide.constant('CONTEXT_PATH', 'bignibou'); // override contextPath here
    }));
    describe('CreateCurriculumCtrl', function () {
        var mockBackend, location, _window;
        beforeEach(inject(function ($rootScope, $controller, $httpBackend, $location, $window) {
            mockBackend = $httpBackend;
            location = $location;
            _window = $window;
            $scope = $rootScope.$new();
            ctrl = $controller('CreateCurriculumCtrl', {
                $scope: $scope
            });
        }));

        it('should redirect to /signin if 401 or 405', function () {
            mockBackend.whenGET('bignibou/utils/findLanguagesByLanguageStartingWith.json?language=fran').respond([{"description":"Français","id":46,"version":0}]);
            mockBackend.whenPOST('bignibou/curriculum/new').respond(function(method, url, data, headers){
                return [401];
            });
            $scope.saveCurriculum();
            mockBackend.flush();
            expect(_window.location.href).toEqual("/bignibou/signin");
        });


    });
});

However, it fails with the following error message:

PhantomJS 1.9.2 (Linux) Controllers CreateCurriculumCtrl should redirect to /signin if 401 or 405 FAILED
    Expected 'http://localhost:9876/context.html' to equal '/bignibou/signin'.
PhantomJS 1.9.2 (Linux) ERROR
    Some of your tests did a full page reload!

I am not sure what is going wrong and why. Can anyone please help?

I just want to ensure the $window.location.href is equal to '/bignibou/signin'.

edit 1:

I managed to get it to work as follows (thanks to "dskh"):

 beforeEach(module('config', function($provide){
      $provide.value('$window', {location:{href:'dummy'}});
 }));
Dan

You can inject stub dependencies when you load in your module:

angular.mock.module('curriculumModule', function($provide){
            $provide.value('$window', {location:{href:'dummy'}});
        });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unit Testing AngularJS Service

From Dev

Unit testing a service in angularJS

From Dev

Unit Testing AngularJS and PouchDB Service

From Dev

Angularjs unit testing service with promise

From Dev

Unit Testing AngularJS and PouchDB Service

From Dev

mock angularjs service for unit testing using jasmine

From Dev

how to mock $window to unit test AngularJS service?

From Dev

AngularJS Unit Testing Controller w/ service dependency in Jasmine

From Dev

Unit testing an AngularJS service with angular-translate calls

From Dev

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

From Dev

AngularJS: Unit Testing Directive w/ Promise returned from a Service

From Dev

AngularJS: Unit Testing Directive w/ Promise returned from a Service

From Dev

Unit testing promises in AngularJS

From Dev

AngularJS unit testing with ReSharper

From Dev

Unit testing controllers in angularJS

From Dev

Cordova & AngularJS unit testing

From Dev

AngularJS Unit testing $location

From Dev

Unit testing Angularjs jasmine

From Dev

AngularJS unit testing with ReSharper

From Dev

Cordova & AngularJS unit testing

From Dev

AngularJS Karma Unit Testing

From Dev

Angularjs Unit Test Not Hitting then part of controller after calling service in AngularJS & testing using Karma & Jasmine

From Dev

Angularjs Unit Test Not Hitting then part of controller after calling service in AngularJS & testing using Karma & Jasmine

From Dev

Angularjs Unit Test for Service

From Dev

Unit testing a service that uses $resource

From Dev

Unit testing AngularJS and Flot Charts

From Dev

AngularJS - Unit testing file uploads

From Dev

AngularJS unit testing. HttpBackend

From Dev

Unit Testing AngularJS route with "resolve"

Related Related

  1. 1

    Unit Testing AngularJS Service

  2. 2

    Unit testing a service in angularJS

  3. 3

    Unit Testing AngularJS and PouchDB Service

  4. 4

    Angularjs unit testing service with promise

  5. 5

    Unit Testing AngularJS and PouchDB Service

  6. 6

    mock angularjs service for unit testing using jasmine

  7. 7

    how to mock $window to unit test AngularJS service?

  8. 8

    AngularJS Unit Testing Controller w/ service dependency in Jasmine

  9. 9

    Unit testing an AngularJS service with angular-translate calls

  10. 10

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

  11. 11

    AngularJS: Unit Testing Directive w/ Promise returned from a Service

  12. 12

    AngularJS: Unit Testing Directive w/ Promise returned from a Service

  13. 13

    Unit testing promises in AngularJS

  14. 14

    AngularJS unit testing with ReSharper

  15. 15

    Unit testing controllers in angularJS

  16. 16

    Cordova & AngularJS unit testing

  17. 17

    AngularJS Unit testing $location

  18. 18

    Unit testing Angularjs jasmine

  19. 19

    AngularJS unit testing with ReSharper

  20. 20

    Cordova & AngularJS unit testing

  21. 21

    AngularJS Karma Unit Testing

  22. 22

    Angularjs Unit Test Not Hitting then part of controller after calling service in AngularJS & testing using Karma & Jasmine

  23. 23

    Angularjs Unit Test Not Hitting then part of controller after calling service in AngularJS & testing using Karma & Jasmine

  24. 24

    Angularjs Unit Test for Service

  25. 25

    Unit testing a service that uses $resource

  26. 26

    Unit testing AngularJS and Flot Charts

  27. 27

    AngularJS - Unit testing file uploads

  28. 28

    AngularJS unit testing. HttpBackend

  29. 29

    Unit Testing AngularJS route with "resolve"

HotTag

Archive