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

沒有使用 jasmine.js 和 sinon.js 調用主干.js 點擊事件

backbone.js click event spy is not getting called using jasmine.js and sinon.js(沒有使用 jasmine.js 和 sinon.js 調用主干.js 點擊事件間諜)
本文介紹了沒有使用 jasmine.js 和 sinon.js 調用主干.js 點擊事件間諜的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試使用backbone.js、jasmine.js 和sinon.js 測試按鈕點擊.但是下面的測試用例失敗了.我正在使用間諜來跟蹤它是否被調用.你能幫我解決這個問題嗎?

I am trying to test a button click using backbone.js, jasmine.js and sinon.js. But the following test case fails. I am using a spy to track whether it is getting called or not. Can you please help me with this?

謝謝.

新任務模板

<script id='new_task_template' type='text/template'>
  <input type='text' id='new_task_name' name='new_task_name'></input>
  <button type='button' id='add_new_task' name='add_new_task'>Add Task</button>
</script>

新任務視圖

T.views.NewTaskView = Backbone.View.extend({
  tagName: 'section',
  id: 'new_task_section',
  template : _.template ( $("#new_task_template").html() ),
  initialize: function(){
    _.bindAll( this, 'render', 'addTask');
  },
  events:{
    "click #add_new_task" : "addTask"
  },
  render: function(){
    $(this.el).html( this.template() );
    return this;
  },
  addTask: function(event){
    console.log("addTask");
  }
});

Jasmine 測試用例

describe("NewTaskView", function(){
  beforeEach( function(){    
    this.view = new T.views.NewTaskView();
    this.view.render();
  });

  it("should #add_new_task is clicked, it should trigger the addTask method", function(){
    var clickSpy = sinon.spy( this.view, 'addTask');
    $("#add_new_task").click();
    expect( clickSpy ).toHaveBeenCalled();
  });
});

茉莉花輸出

NewTaskView
  runEvents
    runshould #add_new_task is clicked, it should trigger the addTask method
      Expected Function to have been called.

推薦答案

問題是你在主干已經將點擊事件直接綁定到 addTask 函數之后添加你的 spy(它在視圖的構建過程中這樣做).因此你的間諜不會被調用.

The problem is you add your spy after backbone has already bound the click event directly to the addTask function (it does that during construction of the View). Therefore your spy will not get called.

嘗試將間諜附加到視圖的原型在構建它之前.像這樣:

Try attaching the spy to a prototype of the View before you construct it. Like this:

this.addTaskSpy = sinon.spy(T.views.NewViewTask.prototype, 'addTaskSpy');
this.view = new T.views.NewTaskView();

然后記得刪除它:

T.views.NewViewTask.prototype.addTaskSpy.restore()

這篇關于沒有使用 jasmine.js 和 sinon.js 調用主干.js 點擊事件間諜的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 欧美高清视频在线观看 | 亚洲色图50p| 久久久做 | 99久久久久久99国产精品免 | 色婷婷亚洲国产女人的天堂 | 久久国产精品一区二区三区 | 欧美成人精品激情在线观看 | 欧美xxxⅹ性欧美大片 | 日本黄色短片 | 毛片在线视频 | 老子午夜影院 | 国内自拍视频在线观看 | 久久激情视频 | 国产精品免费一区二区三区四区 | 欧美久久一区二区三区 | www.日本国产 | 91精品国产乱码麻豆白嫩 | 成人黄色av网址 | 中文字幕观看 | 欧美日韩在线综合 | 日韩欧美中文在线 | 91传媒在线播放 | 日韩三区在线观看 | 黄色av网站在线免费观看 | 欧美精品日韩精品国产精品 | 91视频在线 | 国产精品久久久久婷婷二区次 | 成人av免费在线观看 | 亚洲成人精品一区 | 久久久久国产一区二区三区四区 | 精品久久一区二区 | 国产日韩一区二区三免费高清 | 国产h在线 | 精品亚洲一区二区 | 日韩中文字幕一区 | 国精产品一区一区三区免费完 | 九九热在线视频 | 欧美日韩久久精品 | 亚洲a视 | 91资源在线观看 | 色视频在线播放 |