mocking window.location.href in Javascript -


i have unit tests function makes use of window.location.href -- not ideal far rather have passed in not possible in implementation. i'm wondering if possible mock value without causing test runner page go url.

  window.location.href = "http://www.website.com?varname=foo";       expect(actions.paramtovar(test_data)).toequal("bar");  

i'm using jasmine unit testing framework.

you need simulate local context , create own version of window , window.location objects

var localcontext = {     "window":{         location:{             href: "http://www.website.com?varname=foo"         }     } }  // simulated context with(localcontext){     console.log(window.location.href);     // http://www.website.com?varname=foo }  //actual context console.log(window.location.href); // http://www.actual.page.url/... 

if use with variables (including window!) firstly looked context object , if not present actual context.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -