2012-08-14 13 views
5

私はwindow.location.searchを呼び出す単純な関数をテストしようとしています。私は私の選択のURLを返すことができるようにこの呼び出しをスタブする方法を理解しようとしています。sinon stub for window.location.search

機能:

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, " ")) 

テストケース:

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

これは、もちろん、ない:私のオリジナルの試みはそうのように直接値を返すようにした

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) 

作業。私はスタブを正しく指定しているとは思わないが、それは私の意図を示している。

これを解決する方法についてのご意見は、大変ありがとうございます。

答えて

6

を。私の状況では、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 

その後、私のテストケースでは、私はそうのような私のテストコードで機能を置き換え:CoffeeScriptのを読んでいない人のために

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") 

、同等の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での経験です。作業の解決策はそこに着く旅にほど遠いほど苦痛ではありませんでした。あなたのコメントと貢献に感謝します。

2

UPDATE:そのhttps://groups.google.com/forum/?fromgroups#!topic/sinonjs/MMYrwKIZNUU%5B1-25%5D

この問題を解決するための最も簡単な方法は、ラッパー関数の周りwindow.locationを書くことで、スタブ:window.locationは、それはそう、特殊なケースのビットは、この議論を参照され

sinon.stub(mylib, 'search').returns("myUrl") 

ORIGINAL ANSWER:

mylib.search = function (url) { 
    window.location.search = url; 
}; 

そして、あなたのテストで

このお試しください:前に述べたように、あなたが直接window.locationのモックを作成することができない、だから、

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

私はもう一度試してみましたが、もう一度試しました: 'TypeError:関数として文字列プロパティ検索をラップしようとしました。 ' –

+0

ああ、残念ですが、' window.location.search'は関数ではない文字列です。それをスタブしないでください。スタブを代入で置き換えます: 'window.location.search =" myUrl "'。 –

+0

奇妙なことに、前後にconsole.logを実行すると空白の文字列が表示されるため、何が起きているのか分かりません。割り当ては固執していません。テストケースも表示するようにコードを更新しました。 –

関連する問題