Stubbing window.location.href with Sinon

Francesco Pezzella

I am trying to test some client-side code and for that I need to stub the value of window.location.href property using Mocha/Sinon.

What I have tried so far (using this example):

describe('Logger', () => {
    it('should compose a Log', () => {
        var stub = sinon.stub(window.location, 'href', 'http://www.foo.com');
    });
});

The runner displays the following error:

TypeError: Custom stub should be a function or a property descriptor

Changing the test code to:

describe('Logger', () => {
    it('should compose a Log', () => {
        var stub = sinon.stub(window.location, 'href', {
            value: 'foo'
        });
    });
});

Which yields this error:

TypeError: Attempted to wrap string property href as function

Passing a function as third argument to sinon.stub doesn't work either.

Is there a way to provide a fake window.location.href string, also avoiding redirection (since I'm testing in the browser)?

try-catch-finally

Stubs cannot replace attributes, only functions.

The error thrown reinforces this:

TypeError: Custom stub should be a function or a property descriptor

From the documentation:

When to use stubs?

Use a stub when you want to:

  1. Control a method’s behavior from a test to force the code down a specific path. Examples include forcing a method to throw an error in order to test error handling.

  2. When you want to prevent a specific method from being called directly (possibly because it triggers undesired behavior, such as a XMLHttpRequest or similar).

http://sinonjs.org/releases/v2.0.0/stubs/


Possible solution

While many builtin objects can be replaced (for testing) some can't. For those attributes you could create facade objects which you then have to use in your code and being able to replace them in tests.

For example:

var loc = {

    setLocationHref: function(newHref) {
        window.location.href = newHref;
    },

    getLocationHref: function() {
        return window.location.href;
    }

};

Usage:

loc.setLocationHref('http://acme.com');

You can then in your test write

var stub = sinon.stub(loc, 'setLocationHref').returns('http://www.foo.com');

Note the chained returns() call. There was another error in your code: the third argument has to be a function, not value on another type. It's a callback, not what the attribute should return.

See the source code of stub()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

javascript testing using sinon.js & Qunit. how to test for window.location.href and avoid downloading

From Dev

Stubbing a prototype method with sinon

From Dev

String is not a function on window location href

From Dev

window.location.href not working

From Dev

Mocking / stubbing mongoose findById with sinon

From Dev

Stubbing a get method using Sinon

From Dev

Stubbing a React component method with Sinon

From Dev

stubbing an entire class for testing in sinon

From Dev

Stubbing a promisified function with sinon and bluebird

From Dev

Stubbing Backbone listenTo callbacks with sinon

From Dev

Sinon stubbing a function passed as a parameter

From Dev

Mocking / stubbing mongoose findById with sinon

From Dev

stubbing method with async callback in sinon

From Dev

Sinon stubbing a function passed as a parameter

From Dev

Enable and disable window.location.href

From Dev

onclick window.location.href with input value

From Dev

Javascript window.location.href onClick not working

From Dev

Passing parameter through "window.location.href"

From Dev

onchange event on window.location.href

From Dev

Window.location.href infinite loop

From Dev

window.location.href not working in IE 11

From Dev

Using a variable for if window.location.href.indexOf()

From Dev

$window.location.href NOT WORKING in AngularJS

From Dev

window.location.href appending instead of replacing

From Dev

JavaScript window.location.href not working in localhost

From Dev

window.location.href is not redirecting html page

From Dev

window.location.href not working on IE

From Dev

How to Pass QueryString in window.location.href?

From Dev

onclick window.location.href with input value

Related Related

HotTag

Archive