Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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
Unit testing 主干视图-Mocha-collection.fetch success回调函数中的目标事件_Unit Testing_Backbone.js_Mocha.js_Sinon - Fatal编程技术网

Unit testing 主干视图-Mocha-collection.fetch success回调函数中的目标事件

Unit testing 主干视图-Mocha-collection.fetch success回调函数中的目标事件,unit-testing,backbone.js,mocha.js,sinon,Unit Testing,Backbone.js,Mocha.js,Sinon,在onBeforeShow函数中测试木偶组合视图m时,我正在执行收集。使用成功回调获取。但是在回调中,我继续克隆我的集合并将事件附加到它 现在,我的问题是,在测试此视图时,如何将在collection.fetch的success回调中设置的各种事件/函数调用作为目标? 这是可能的,还是不仅仅是因为不能进行代码重构,而是应该进行代码重构 如果我在checkEmptyState上设置sinon spy,然后向为测试创建的集合中添加一个模型,则不会触发spy,这很有意义,因为我在beforeach函数

onBeforeShow
函数中测试
木偶组合视图
m时,我正在执行
收集。使用
成功
回调获取
。但是在回调中,我继续克隆我的集合并将事件附加到它

现在,我的问题是,在测试此视图时,如何将在
collection.fetch的
success
回调中设置的各种事件/函数调用作为目标? 这是可能的,还是不仅仅是因为不能进行代码重构,而是应该进行代码重构

如果我在
checkEmptyState
上设置
sinon spy
,然后
向为测试创建的集合中添加一个模型,则不会触发
spy
,这很有意义,因为我在
beforeach
函数中为集合设置了
fetch
方法,以防止任何api调用。如果可能,哪种方法/设置是在
success
回调中测试代码的最佳方法/设置

例如:

onBeforeShow: function () {

        var that = this;

        this.fetch = this.collection.fetch({
            success: function (collection, response, options) {

                if (!response.length ) {
                    that.addEmptyPostsMessage();
                }

                that.stopListening(that.collection);

                //Change collection property and re-apply events
                that.collection = that.collection.clone(that.options.filterAttr, that.options.isFiltered);
                that._initialEvents();

                that.collection.reset(that.collection.filterItems(that.collection.models, that.options.filterAttr), {reset: true});

                that.listenTo(that.collection, 'update', that.checkEmptyState);

            },
            reset: false,
            remove: false
        });
    }
对于我的测试设置:

beforeEach(function () {

  this.server = sinon.fakeServer.create();
  this.server.autoRespond = true;

  this.collectionFetchStub = sinon.stub(My.Collection.Items.prototype, 'fetch', function () {
    return: {
             success: function () {console.log("this is never called!")}
            }
});

我的
存根中的
成功
回调从未到达。

您可以确保获取存根时执行它的成功回调。差不多

var collection = [...],
    response = 'ok';

this.collectionFetchStub = sinon.stub(My.Collection.Items.prototype, 'fetch', function (options) {
    options.success(collection, response);
};
或者当使用sinon fakeserver而不是autoRespond时,您应该尝试一下

server.respondWith([200, { 'Content-Type': 'text/html', 'Content-Length': 2 }, collection_json_as_string]);

确保响应是有效的json。

我曾尝试使用假服务器,但它仍然没有触发成功回调,即使我在服务器响应中返回了200 OK。获取存根只是collection.prototype获取方法上的存根。我知道这不会触发回调。我将尝试将成功回调添加到存根本身,并查看其工作原理。我再次查看了获取参数并编辑了我的答案。我已更新了我的问题。即使我存根我的fetch方法并让它返回一个success函数,由于某种原因,这永远不会达到。可能是因为它在
onBeforeShow
功能中?我尝试在测试套件中手动调用
onBeforeShow
,但始终没有到达成功回调。