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

如何斷言使用茉莉花點(diǎn)擊事件調(diào)用間諜?

How to assert a spy is invoked with event on click using jasmine?(如何斷言使用茉莉花點(diǎn)擊事件調(diào)用間諜?)
本文介紹了如何斷言使用茉莉花點(diǎn)擊事件調(diào)用間諜?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

我正在編寫(xiě)一個(gè)簡(jiǎn)單的點(diǎn)擊處理程序,需要傳入事件(像這樣)

I'm writing a simple click handler and need the event passed in (like so)

Thing = function($){

    var MyObject = function(opts){
        this.opts = opts;
    };

    MyObject.prototype.createSomething = function(){
        var that = this;
        $('#some_dom_element').live('click', function(e) {
            that.doStuff(e);
        });
    };

    MyObject.prototype.doStuff = function(e) {
        //do some javascript stuff ...
        e.preventDefault();
    };

    return MyObject;

}(jQuery);

目前在我的 jasmine 規(guī)范中,我有一些東西可以監(jiān)視我期望被調(diào)用的函數(shù)(但是因?yàn)樗怯?e 調(diào)用的——不是沒(méi)有 args——我的斷言失敗了)

Currently in my jasmine spec I've got something to spy on the function I expect gets invoked (but since it's called with e -not without args- my assertion is failing)

    it ("live click handler added to the dom element", function(){
        var doSpy = spyOn(sut, 'doStuff');
        sut.createSomething();
        $("#some_dom_element").trigger('click');
        expect(doSpy).toHaveBeenCalledWith();
    });

如何更正此toHaveBeenCalledWith"以按預(yù)期工作?

How can I correct this "toHaveBeenCalledWith" to work as I expect?

更新

我無(wú)法按原樣獲得公認(rèn)的答案,但我可以稍微改變它,以下是我的 100% 工作示例

I couldn't get the accepted answer to work as is but I was able to alter it just a little and the below is my 100% working example

    it ("should prevent default on click", function(){
        var event = {
            type: 'click',
            preventDefault: function () {}
        };
        var preventDefaultSpy = spyOn(event, 'preventDefault');
        sut.createSomething();
        $("#some_dom_element").trigger(event);
        expect(preventDefaultSpy).toHaveBeenCalledWith();
    });

推薦答案

你必須觸發(fā)你自己的事件,為 stopPropagation 方法傳遞一個(gè)間諜,因?yàn)槟阆霚y(cè)試事件是否被停止.

You have to trigger your own event passing a spy for the stopPropagation method, cause you wanna test if the event was stopped.

var event = {
    type: 'click',
    stopPropagation: function(){}
}
var spy = spyOn(event, 'stopPropagation');
$('#some_dom_element').trigger(event);
expect(spy).toHaveBeenCalled();

注意:當(dāng)您監(jiān)視要測(cè)試的對(duì)象時(shí),會(huì)產(chǎn)生代碼異味,因?yàn)槟_(kāi)始測(cè)試類(lèi)的內(nèi)部行為.把你的功能想象成一個(gè)黑盒子,只測(cè)試你放入和取出的東西.在您的情況下,重命名函數(shù)會(huì)破壞測(cè)試,而代碼仍然有效.

Note: there is code smell when you spy on the object you want to test, because you start to test the inner behavior of your class. Think about your function as a black box and test only the things you put in and get out. In your case, renaming the function in will break the test, while the code is still valid.

這篇關(guān)于如何斷言使用茉莉花點(diǎn)擊事件調(diào)用間諜?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認(rèn)為文檔“準(zhǔn)備好之前,如何讓我的 jasmine 測(cè)試裝置加載?) - IT屋-程序員軟件開(kāi)發(fā)技術(shù)
What do jasmine runs and waitsFor actually do?(jasmine 運(yùn)行和等待實(shí)際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來(lái)更改 lt;input type=filegt; 的事件用于單元測(cè)試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對(duì)鏈?zhǔn)椒椒ㄟM(jìn)行單元測(cè)試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測(cè)試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監(jiān)視函數(shù)中的函數(shù)調(diào)用?)
主站蜘蛛池模板: 综合九九 | 欧美精品一区三区 | 久久久久久成人 | 中文字幕免费在线 | 国产在线观看一区二区 | 天天干天天插天天 | 亚洲高清视频一区二区 | 亚洲午夜小视频 | 一道本不卡视频 | 成人在线观看免费视频 | 成人精品啪啪欧美成 | 精品福利在线 | 久久久精品网站 | 亚洲成人福利视频 | 一级在线观看 | 成人免费视频观看视频 | 国产精品自产拍在线观看蜜 | 中文字幕亚洲视频 | 日韩一区二区av | 色在线免费 | 日本三级电影在线看 | 丝袜美腿一区二区三区动态图 | 午夜视频在线免费观看 | 亚洲国产电影 | 亚洲精品国产一区 | 91久久精品一区二区二区 | 狠狠婷婷综合久久久久久妖精 | 亚洲 欧美 日韩在线 | 欧美久久一区 | 欧美一区二区三区一在线观看 | 精品久久久久久亚洲精品 | 亚洲网在线 | 中文字幕一区二区三区精彩视频 | 国产精品毛片在线 | 色综合99 | 欧美日韩在线成人 | 精品国产免费一区二区三区五区 | 欧美成人精品一区二区男人看 | 国产激情视频在线免费观看 | 四虎在线播放 | www.日韩av.com|