Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Coffeescript window.location.search的Sinon.JS存根_Coffeescript_Stubbing_Sinon_Buster.js - Fatal编程技术网

Coffeescript window.location.search的Sinon.JS存根

Coffeescript window.location.search的Sinon.JS存根,coffeescript,stubbing,sinon,buster.js,Coffeescript,Stubbing,Sinon,Buster.js,我正在尝试测试一个调用window.location.search的简单函数。我正在尝试理解如何存根这个调用,以便返回我选择的url 功能: getParameterByName: (name) => name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]") regexS = "[\\?&]" + name + "=([^&#]*)" regex = new RegExp(regexS)

我正在尝试测试一个调用window.location.search的简单函数。我正在尝试理解如何存根这个调用,以便返回我选择的url

功能:

getParameterByName: (name) =>    
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]")
  regexS = "[\\?&]" + name + "=([^&#]*)"
  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, " "))
测试用例:

describe "Data tests", () ->
  it "Should parse parameter from url", () ->        
    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)
我最初的尝试是直接返回一个值,如下所示:

sinon.stub(window.location.search).returns("myUrl")
当然,这是行不通的。我认为我没有正确指定存根,但它显示了我的意图


如果您有任何关于如何解决此问题的想法,我们将不胜感激。

更新:
窗口。位置似乎有点特殊,请参阅以下讨论:

解决此问题的最简单方法是在
window.location
周围编写一个包装器函数,并将其存根:

mylib.search = function (url) {
  window.location.search = url;
};
在你的测试中:

sinon.stub(mylib, 'search').returns("myUrl")
原始答复:

试试这个:

sinon.stub(window.location, 'search').returns("myUrl")

所以,正如前面提到的,您不能直接模拟window.location。mylib.search包装器的想法也不适合我的情况。因此,我所做的就是将对
window.location.search
的调用中断到它自己的函数中。我的新课看起来是这样的:

getParameterByName: (name) =>
  console.log("name: #{name}")
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]")
  regexS = "[\\?&]" + name + "=([^&#]*)"
  regex = new RegExp(regexS)
  results = regex.exec(@getWindowLocationSearch())
  if(results == null)
    return ""
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "))

getWindowLocationSearch:() =>
  window.location.search
然后在我的测试用例中,我用测试代码替换函数,如下所示:

describe "Data tests", () ->
  it "Should parse parameter from localhost url", () ->
    goodUrl = "http://localhost:3333/?token=val1"

    Data::getWindowLocationSearch = () -> return goodUrl
    unit = new Data()
    result = unit.getParameterByName("token")

    expect(result).toBe("val1")
对于那些不阅读Coffeescript的人,下面列出了等效的javascript代码:

it("Should parse parameter from localhost url", function() {
  var goodUrl, result, unit;
  goodUrl = "http://localhost:3333/?token=val1";
  Data.prototype.getWindowLocationSearch = function() {
    return goodUrl;
  };
  unit = new Data();
  result = unit.getParameterByName("token");
  expect(result).toBe("val1");
  return expect(true).toBe(true);
});

这是我通常使用Javascript的经验。有效的解决方案远没有到达那里的旅程那么痛苦。非常感谢您的评论和贡献。

我以前试过,现在又试了一次:
TypeError:试图将字符串属性搜索包装为函数
哦,对不起,
window.location.search
是字符串而不是函数,因此您不能将其存根。将存根替换为赋值:
window.location.search=“myUrl”
。奇怪的是,在前后执行console.log会导致一个空白字符串,因此我不确定发生了什么。任务没有完成。我更新了我的代码以显示我的测试用例。好的,我认为这实际上比我最初想象的要难。请参阅此讨论:这在我的类中不起作用,因为我必须使用原型来覆盖函数。因为这个原因,西农的存根不能在我的班上使用。