2014-11-14 19 views
0

假設我想加入我自己的特殊存根擴展興農: 我可以只寫:如何使用自定義方法和字段擴展沙盒興農

sinon.specialStub = function() { 
    return this.stub().returns('SPECIAL'); 
}; 

或者,我可以用sinon.extend和寫類似:

sinon.extend(sinon, { 
     specialStub: function() { 
     return this.stub().returns('SPECIAL'); 
     } 
    }); 

這樣做基本上是一樣的。

一切都會按預期工作,但調用var sandbox = sinon.sandbox.create();這種方法將會失敗。

這裏是唯一已知的解決方法,我能想到的:

var sandbox = sinon.extend(sinon.sandbox.create(), { 
     specialStub: function() { 
     return this.stub().returns('SPECIAL'); 
     } 
}); 

的quesion是 - 沒有任何一個知道如何擴展興農和看到沙盒的擴展方法是什麼?

回答

1

我知道的唯一方法是將方法添加到sinon.collection

sinon.collection.specialStub = function() { return this.stub().returns('Special'); } 
var sandbox = sinon.sandbox.create(); 
console.log(sandbox.specialStub); 

可能會考慮要求sinon的維護人員發現它是否易碎,因爲它觸及了它們認爲是圖書館內部的東西。