title: Sinon.JS stub for window.location.search
body: <p>I am trying to test a simple function that makes a call to window.location.search.  I'm trying to understand how to stub this call so that I can return a url of my choosing.</p>
  
  <p>function:</p>
  
  <pre><code>getParameterByName: (name) =&gt;    
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]")
    regexS = "[\\?&amp;]" + name + "=([^&amp;#]*)"
    regex = new RegExp(regexS)    
    results = regex.exec(window.location.search) //Stub call to window.location.search
    if(results == null)
      return ""
    else
      return decodeURIComponent(results[1].replace(/\+/g, " "))
  </code></pre>
  
  <p>Test case:</p>
  
  <pre><code>describe "Data tests", () -&gt;
    it "Should parse parameter from url", () -&gt;        
      data = new Data()
  
      console.log("search string: " + window.location.search) //prints "search string:"
      window.location.search = "myUrl"
      console.log("search string: " + window.location.search) //prints "search string:"
      console.log(data.getParameterByName('varName'))
  
      expect(true).toBe(true)
  </code></pre>
  
  <p>My original attempt was to return a value directly like so:</p>
  
  <pre><code>sinon.stub(window.location.search).returns("myUrl")
  </code></pre>
  
  <p>This, of course, doesn't work. I don't think I'm specifying the stub correctly, but it shows my intent.</p>
  
  <p>Any ideas on how to solve this would be greatly appreciated.</p>