Reputation: 3080
Having read the official documentation and searched, but I still have no idea about this.
Simple Source Code:
// obj is an object with an array element
// each element in array has its function
if (obj.arr['key1']) {
return obj.arr['key1'].getValue();
}
What I want is controlling obj.arr
, for example:
var stub = sinon.stub(obj, "arr");
stub['key2'].returns = {...} //add new Index
delete stub['key1'].returns //remove old Index
Upvotes: 2
Views: 4499
Reputation: 2850
You can stub a function in an array like this:
myObj = {
myArray: [
function(){},
function(){},
function(){}
]
}
var stub = sinon.stub(myObj.myArray, [0]).returns() //insert what should be returned
Use:
describe ('foo', function () {
it ('foo', sinon.test(function () {
this.stub (myObj.myArray, [0]).returns();
}))
}
if you want automatic cleanup after your stubs.
Upvotes: 2