Angularjs: mock location.path() with spyOn for a unit test

Simon

I have already read this post (and others) but I don't manage to make this simple unit test work. I'm using the version 2 of Jasmine. My factory is very simple:

angular.module('myApp')
    .factory('detectPath', function ($location, $rootScope) {
        'use strict';
        var locationPath = $location.path()
        function getPath () {
            if (locationPath === '/') {
                locationPath = 'home';
            } else {
                locationPath = '';
            }
            $rootScope.path = locationPath;
        }
        getPath();
        return locationPath;
    });

And my unit test is just as simple:

'use strict';
describe('Factory: detectPath', function () {
    var detectPath, $rootScope, $location;

    beforeEach(module('myApp'));
    beforeEach(inject(function (_detectPath_, _$rootScope_, _$location_) {
        detectPath = _detectPath_;
        $rootScope = _$rootScope_;
        $location = _$location_;
        spyOn($location, 'path').and.returnValue('/');
    }));

    it('should return pathName', function ($location) {
        expect($rootScope.path).toBe('home');
    });
});

This doesn't pass the test (I get the error expect false to be "home").

What I am doing wrong? Is there a way to verify that spyOn has been called (only once)?

fracz

There are two main problems with your code.

First of all, your getPath() function is executed before you are setting spy. You should either set the spy in the previous beforeEach or inject your factory in the test (I went for the second solution).

The second problem (which does not influence the test yet) is that you hide your $location variable with test's function argument - you will not be able to access it as it will always be undefined. After I removed this arg, I'm able to test if spy has been called with expect(...).toHaveBeenCalled().

Here is a working code:

describe('Factory: detectPath', function () {
    var detectPath, $rootScope, $location;

    beforeEach(module('myApp'));
    beforeEach(inject(function (_$rootScope_, _$location_) {
        $rootScope = _$rootScope_;
        $location = _$location_;
        spyOn($location, 'path').and.returnValue('/');
    }));

    it('should return pathName', function () {
        inject(function (detectPath) {
            expect($location.path).toHaveBeenCalled();
            expect($rootScope.path).toBe('home');
        });
    });
});

And JSFiddle (using Jasmine 1.3 but the only difference in this sample is that you call and.returnValue in Jasmine 2 and returnValue in Jasmine 1.3).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angularjs: mock location.path() with spyOn for a unit test

From Dev

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

From Dev

Cannot read property 'spyOn' of null - Mocking a promise in angularJS unit test

From Dev

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

From Dev

how to mock $window to unit test AngularJS service?

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

Unit Test Expect SpyOn Not Found

From Dev

Unit test for angularjs with $window.location.pathname

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

AngularJS + Jasmine mock unit test $http factory of POST method

From Dev

Mock IMemoryCache in unit test

From Dev

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

From Dev

AngularJS Unit testing $location

From Dev

How to spyOn a service to test it -- AngularJS/Jasmine

From Dev

Mock/Stub a RuntimeException in unit test

From Dev

Spock Mock not working for unit test

From Dev

How to mock springSecurityService in an unit test

From Dev

How to mock this unit test in Python?

From Dev

Unit test a void method with Mock?

From Dev

AngularJS and location.path()

From Dev

Unit test of controller angularjs

From Dev

AngularJS - Unit Test with $timeout

From Dev

Unit test angularjs controller

From Dev

AngularJs unit test 'this'

From Dev

Angularjs Unit Test for Service

Related Related

HotTag

Archive