dimanche 28 juin 2015

mocking location.prototype in javascript for jasmine unit testing

The following code is working for mocking the userAgent string for unit testing in Jasmine.

it('check browser name and version',function(){

    var __originalNavigator = navigator;
    var navigator = new Object();
    navigator.__proto__ = __originalNavigator;
    navigator.__defineGetter__('userAgent', function () { return 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20130401 Firefox/31.0'; });

    var isFirefox = browserCheck.isFirefox();
    expect(isFirefox).toBe(true);

});

Where as the similar following code is not working.

it('check browser security protocol',function(){
        var __originalPrototype = window.location;
        window.location = new Object();
        window.location.__proto__ = __originalPrototype;
        window.location.__defineGetter__('protocol', function () {     return 'https:'; });

        var isSecure = BrowserCheck.checkSecurity();
                expect(isFirefox).toBe(true);
});

My question is mainly about mocking the window.location.

Aucun commentaire:

Enregistrer un commentaire