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

Jasmine 不能監視事件處理程序?

Jasmine can#39;t spy on event handler?(Jasmine 不能監視事件處理程序?)
本文介紹了Jasmine 不能監視事件處理程序?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

嘗試使用 Jasmine 測試在單擊的元素上調用事件處理程序.有一個Pad"對象,其中包含一個被點擊的 DOM 元素PadElement".事件處理程序是 Pad 對象上的一個方法:

Trying to test that an event handler gets called on a clicked element with Jasmine. Have a "Pad" object that contains a DOM element "PadElement", which gets clicked. The event handler is a method on the Pad object:

GRAPH.Pad = function(graphDiv, graph) {
    this.graph = graph;

    this.clickHandler = function(e) {
        console.log('padElement clickHandler called');
        //this.graph.createVertex(e.clientX, e.clientY);
    };
    this.padElement = GRAPH.padElement(graphDiv, this.clickHandler);
}

GRAPH.padElement = function(graphDiv, clickHandler) {
    //Initialize pad
    var NS="http://www.w3.org/2000/svg";
    var pad=document.createElementNS(NS,"svg");
    pad.setAttributeNS(null, 'id', 'pad');
    graphDiv.appendChild(pad);
    pad.addEventListener('click', clickHandler)
    return pad;
}

茉莉花測試:

var testDiv = document.createElement('div');
var testGraph = new GRAPH.Graph(testDiv);
var testPad = new GRAPH.Pad(testDiv, testGraph);

  it('has its clickHandler function called when its padElement is clicked',
    function() {
      spyOn(testPad, "clickHandler");
      simulateClick(testPad.padElement);
      //testPad.clickHandler();
      expect(testPad.clickHandler).toHaveBeenCalled();
  });

但是,測試失敗.請注意,事件偵聽器確實被調用(console.log 通過鼠標單擊和模擬單擊成功寫入),并且如果我直接調用 testPad.clickHandler(),Jasmine 的間諜可以拾取它.但是在實際測試中會發生什么?事件處理程序調用是否在運行時轉移到不同的對象?這樣做的正確方法是什么?

However, the test FAILS. Note that the event listener does get called (console.log writes successfully with a mouse click and with simulateClick), AND if I just call the testPad.clickHandler() directly Jasmine's spy can pick it up. But what happens during the actual test? Does the event handler invocation get transferred to a different object at runtime? What's the right way to do this?

推薦答案

您實際上是在測試 GRAPH.padElement 調用提供的 clickHandler 而不是 thisGRAPH.Pad 的.clickHandlerGRAPH.padElement 調用.我會怎么做

You are actually testing that GRAPH.padElement calls the supplied clickHandler and not that this.clickHandler of GRAPH.Pad is called by GRAPH.padElement. How I would do that is

var testDiv = document.createElement('div');
var clickHandlerSpy = jasmine.CreateSpy();
var padelement = padElement(testDiv , clickHandlerSpy);

  it('has its clickHandler function called when its padElement is clicked',
    function() {
      simulateClick(testPad.padElement);
      expect(clickHandlerSpy).toHaveBeenCalled();
  });

這聽起來可能與您想要實現的目標略有不同.但是在理想的單元測試世界中,你應該獨立測試每個單元,所以我會首先測試 padElement 是否做了它應該做的事情(如上所述),然后編寫另一個測試來確保 GRAPH.Pad 正在將正確的處理程序傳遞給 padElement.現在要做到這一點,我不會直接從 GRAPH.Pad 內部創建 padElement ,而是以某種方式從外部注入它,然后在 jasmine 規范中對其進行模擬.如果您對這部分不清楚,請告訴我,我可以為您整理一些代碼.

This may sound little different to what you are trying to achieve. But in ideal unit testing world you should be testing each unit independently, so I would first test that padElement does what it's supposed to do (as above) and then write another test to make sure GRAPH.Pad is passing correct handler to padElement. Now to do that I would not create padElement directly from within GRAPH.Pad but somehow inject it from outside and then mock it in the jasmine specs. If you are not clear on this part let me know and I can put some code together for you.

這篇關于Jasmine 不能監視事件處理程序?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 亚洲精品久久久一区二区三区 | 国产精品精品视频一区二区三区 | 欧美精品一区二区三区在线播放 | 伊人网伊人 | 精品久久久久久久久久久久久久久久久 | 国产精品一区二区在线观看 | 精品一区二区av | 精品一区二区三区在线观看 | 国产免国产免费 | 国产精品一卡 | 一二区视频| 欧美一级二级三级视频 | 毛片网站在线观看 | 日韩欧美国产精品 | 日韩在线国产 | 欧美精品一级 | 国产精品海角社区在线观看 | 免费黄网站在线观看 | 欧美日本亚洲 | 99re在线视频 | 97在线观看| 中国美女一级黄色片 | 97国产超碰 | 精品视频在线播放 | 国产欧美精品区一区二区三区 | 亚洲人成在线播放 | 日韩成人免费av | 国产精品视频不卡 | 成人免费看片网 | 国产一区在线免费观看视频 | 亚洲精品国产成人 | 最新超碰 | av在线伊人 | 午夜免费视频 | 久热精品在线观看视频 | 欧美九九 | 成年人精品视频 | 国产亚洲一区二区在线观看 | 亚州精品天堂中文字幕 | 天天干天天色 | 99久久精品免费看国产四区 |