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 如何在使用RequireJS(和Jasmine/Sinon)时在另一个视图呈现方法中实例化主干视图_Unit Testing_Backbone.js_Requirejs_Jasmine_Sinon - Fatal编程技术网

Unit testing 如何在使用RequireJS(和Jasmine/Sinon)时在另一个视图呈现方法中实例化主干视图

Unit testing 如何在使用RequireJS(和Jasmine/Sinon)时在另一个视图呈现方法中实例化主干视图,unit-testing,backbone.js,requirejs,jasmine,sinon,Unit Testing,Backbone.js,Requirejs,Jasmine,Sinon,我试图使用Jasmine和Sion编写单元测试,但在使用RequireJs加载模块时,我很难找到以下等效项: sinon.stub(window, "MyItemView"); 当使用RequireJs时,我无法以这种方式存根,因为MyItemView未连接到窗口 以下是我何时需要MyItemView的示例: var MyView = Backbone.View.extend({ el: '#myElement', initialize : function() { var that

我试图使用Jasmine和Sion编写单元测试,但在使用RequireJs加载模块时,我很难找到以下等效项:

sinon.stub(window, "MyItemView");
当使用RequireJs时,我无法以这种方式存根,因为MyItemView未连接到窗口

以下是我何时需要MyItemView的示例:

var MyView = Backbone.View.extend({
el: '#myElement',
initialize : function() {
    var that = this;
    this.collection.fetch({
        success : function() {
            that.render();
        }
    });
},

render : function() {
    this.collection.each(this.renderItem);
}

renderItem: function(model){
        var myItemView = new MyItemView({model: model});
        $('#myElement').append(myItemView.render().el);
    },

...
现在,我可以使用Jasmine测试innerHtml是否包含预期的HTML:

it('View should contain correct Html', function(){
            var collectionFetchStub = sinon.stub(this.myCollection, 'fetch').yieldsTo('success', this.myCollection);
            this.view = new MyView({collection: this.myCollection});
            expect(this.view.el.innerHTML).toContain('<li><a href=""> 1 : test </a></li>');

            // Remove Stubs
            collectionFetchStub.restore();
        });
it('视图应包含正确的Html',函数(){
var collectionFetchStub=sinon.stub(this.myCollection,'fetch').yieldsTo('success',this.myCollection);
this.view=newmyview({collection:this.myCollection});
expect(this.view.el.innerHTML).toContain('
  • '); //清除存根 collectionFetchStub.restore(); });
    但是,此测试依赖于MyItemView的呈现,这对于单元测试来说并不理想。这个问题的最佳解决方案是什么?我是javascript的新手,解决方案似乎很复杂。

    看看如何使用requireJS存根依赖项。有一些解决办法。比如TestRJ、squireJs或我自己的小解决方案。其主要思想是用spy/stub/mock覆盖requireJs依赖项,这样您就可以只测试模块了

    因此,在您的情况下,您可以像这样存根MyItemView:

    var el = $("<div>test</div>")
    var MyItemView = sinon.stub().returns({render: sinon.stub().returns(el)})
    
    var el=$(“测试”)
    var MyItemView=sinon.stub().returns({render:sinon.stub().returns(el)})
    

    然后,您必须将MyItemView存根注入到您的require上下文中,并且您可以测试附加到
    $(“#myElement”)的测试div
    。这是不理想的,因为所有DOM的东西,但它会工作。更好的方法是不要渲染主干视图之外的内容。因为你可以在视图中注入一个模拟的
    el
    ,然后测试你的模拟的append方法是否被调用。

    我可以看到你在说什么,但是如果你在测试
    MyView
    ,那么你就不必关心它是如何组成的(或者你会这么做)。此版本的
    MyView
    使用
    MyItemView
    ,但如果您重写
    MyView
    ,则可能不会。那么问题是,您对
    MyView
    的原始测试是否仍然有效?如果它们不是,那么你可能在测试一些你不需要(或不应该)的东西。