sinon stub一个数组对象

时间:2016-10-11 13:07:28

标签: javascript sinon

阅读了官方文档并进行了搜索,但我仍然不知道这一点。

简单的源代码:

@using (Ajax.BeginForm("Example", "Example", null, new AjaxOptions
{
    HttpMethod = "POST",
    OnBegin = "ajaxFormBegin",
    OnSuccess = "ajaxFormSuccessWrapper"
}

<script>
function ajaxFormSuccessWrapper(data, status, xhr) {
  var form = this; 
  ajaxFormSuccess(data, status, xhr, 'exampleValue'); 
}
</script>

我想要的是控制// obj is an object with an array element // each element in array has its function if (obj.arr['key1']) { return obj.arr['key1'].getValue(); } ,例如:

obj.arr

1 个答案:

答案 0 :(得分:2)

你可以在这样的数组中存根函数:

myObj = {

    myArray: [
    function(){}, 
    function(){}, 
    function(){}
    ]
}

var stub = sinon.stub(myObj.myArray, [0]).returns() //insert what should be returned

使用:

describe ('foo', function () {

    it ('foo', sinon.test(function () {

        this.stub (myObj.myArray, [0]).returns();

    }))
}

如果你想在你的存根之后自动清理。