久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Sinon 似乎沒有監視事件處理程序回調

Sinon does not seem to spy for an event handler callback(Sinon 似乎沒有監視事件處理程序回調)
本文介紹了Sinon 似乎沒有監視事件處理程序回調的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在使用 Jasmin、Simon 和 jasmin-simon 測試主干視圖.

I'm testing a backbone view with Jasmin, Simon and jasmin-simon.

代碼如下:

var MessageContainerView = Backbone.View.extend({
    id: 'messages',
    initialize: function() {
        this.collection.bind('add', this.addMessage, this);
    },
    render: function( event ) {
        this.collection.each(this.addMessage);
        return this;
    },
    addMessage: function( message ) {
        console.log('addMessage called', message);
        var view = new MessageView({model: message});
        $('#' + this.id).append(view.render().el);
    }
});

實際上,我所有的測試都通過了,但只有一個.我想檢查是否在向 this.collection 添加項目時調用了 addMessage.

Actually, all my tests pass but one. I would like to check that addMessage is called whenever I add an item to this.collection.

describe('Message Container tests', function(){
    beforeEach(function(){
        this.messageView = new Backbone.View;
        this.messageViewStub = sinon.stub(window, 'MessageView').returns(this.messageView);

        this.message1 = new Backbone.Model({message: 'message1', type:'error'});
        this.message2 = new Backbone.Model({message: 'message2', type:'success'});
        this.messages = new Backbone.Collection([
            this.message1, this.message2            
        ]); 

        this.view = new MessageContainerView({ collection: this.messages });
        this.view.render();

        this.eventSpy = sinon.spy(this.view, 'addMessage');
        this.renderSpy = sinon.spy(this.messageView, 'render');
        setFixtures('<div id="messages"></div>');
    });
    afterEach(function(){
        this.messageViewStub.restore();
        this.eventSpy.restore();
    });

    it('check addMessage call', function(){
        var message = new Backbone.Model({message: 'newmessage', type:'success'});
        this.messages.add(message);

        // TODO: this fails not being called at all
        expect(this.view.addMessage).toHaveBeenCalledOnce();
        // TODO: this fails similarly
        expect(this.view.addMessage).toHaveBeenCalledWith(message, 'Expected to have been called with `message`');
        // these pass
        expect(this.messageView.render).toHaveBeenCalledOnce();
        expect($('#messages').children().length).toEqual(1);
    });
});

如您所見,確實調用了 addMessage.(它會登錄到控制臺并按應有的方式調用 this.messageView.在監視 addMessage 調用時我錯過了什么?

As you can see addMessage is called indeed. (It logs to the console and it calls this.messageView as it should. What do I miss in spying for addMessage calls?

謝謝,維克托

推薦答案

我不確定,但據我了解,會發生以下情況:

I'm not quit sure but, as I understand it, the following happens:

  1. 您創建一個調用 initialize 函數的新視圖,并將您的 view.addMessage 綁定到您的集合.
  2. 執行此操作后,Backbone 獲取該函數并將其存儲在您集合的事件存儲中.
  3. 然后你監視 view.addMessage 這意味著你用一個監視函數覆蓋它.這樣做不會影響收集事件存儲中存儲的函數.
  1. You create a new view which calls the initialize function and bind your view.addMessage to your collection.
  2. Doing this, Backbone take the function and store it in the event store of your collection.
  3. Then you spy on view.addMessage which means you overwrite it with a spy function. Doing this will have no effect on the function that is stored in the collection event store.

所以他們的測試存在一些問題.您的視圖有很多您沒有模擬出來的依賴項.您創建了一堆額外的 Backbone 模型和集合,這意味著您不僅要測試您的視圖,還要測試 Backbones 集合和模型的功能.

So their are some problems with your test. You view has a lot of dependencies that you not mock out. You create a bunch of additional Backbone Models and Collections, which means you not test only your view but also Backbones Collection and Model functionality.

您不應該測試 collection.bind 是否可以工作,而是您已經使用參數 'add', this.addMessage 對集合調用了 bind, 這個

You should not test that collection.bind will work, but that you have called bind on the collection with the parameters 'add', this.addMessage, this

initialize: function() {
    //you dont 
    this.collection.bind('add', this.addMessage, this);
},

所以,模擬集合很容易:

So, its easy to mock the collection:

var messages = {bind:function(){}, each:function(){}}
spyOn(messages, 'bind');
spyOn(messages, 'each');
this.view = new MessageContainerView({ collection: messages });

expect(message.bind).toHaveBeenCalledWith('bind', this.view.addMessage, this.view);

this.view.render()

expect(message.each).toHaveBeenCalledWith(this.view.addMessage);

... and so on

這樣做你只測試你的代碼,而不依賴于 Backbone.

Doing it this way you test only your code and have not dependencies to Backbone.

這篇關于Sinon 似乎沒有監視事件處理程序回調的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 国产一区不卡 | 色视频在线播放 | 亚洲一区二区免费视频 | 伊人久久伊人 | 日本免费一区二区三区四区 | 欧美日韩亚洲一区二区 | ww亚洲ww亚在线观看 | 欧美激情视频一区二区三区在线播放 | 免费一区在线 | 伊人久久大香线 | 久久高清 | 久久免费精彩视频 | 91大神在线看 | 91久久精品国产 | 国产精品日本一区二区不卡视频 | 99伊人| 成人无遮挡毛片免费看 | 国产精品视频网 | 欧美激情视频网站 | 国产精品特级毛片一区二区三区 | 欧美久久一区二区三区 | 久久久久免费精品国产小说色大师 | 国产高清免费 | 拍真实国产伦偷精品 | 91精品国产综合久久婷婷香蕉 | 久久亚洲国产精品 | 日韩欧美综合在线视频 | 免费三级网站 | 91视频在线看 | 日韩av啪啪网站大全免费观看 | 在线免费观看a级片 | 中文字幕 在线观看 | 亚洲电影一区二区三区 | 亚洲91精品 | 国产成人福利在线 | 国产又色又爽又黄又免费 | 精品国产乱码久久久久久丨区2区 | 亚洲欧美日韩中文在线 | 四虎永久 | 一久久久 | 精品91|