久久久久久久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 - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 国产视频中文字幕 | 精品在线99 | 欧美成人a∨高清免费观看 色999日韩 | 日本精品一区二区 | 日韩在线不卡视频 | av先锋资源| 欧美在线综合 | 黄网站在线观看 | 青娱乐一区二区 | 91天堂| 日韩在线视频免费观看 | www.国产.com| 九七午夜剧场福利写真 | 国产精品伦理一区 | 日韩视频中文字幕 | 欧美日韩网站 | 亚洲欧美日韩精品久久亚洲区 | 亚洲欧美日韩精品久久亚洲区 | 欧美日韩综合一区 | 日韩不卡视频在线 | 国产av毛片 | 久久综合香蕉 | 成人影院网站ww555久久精品 | 久久国产精品亚洲 | 欧美日韩综合 | 超碰人人人人 | 日韩国产中文字幕 | 色婷婷av一区二区三区软件 | 在线观看的av | av毛片 | 国产伦一区二区三区四区 | 在线观看免费毛片 | 日韩成人在线观看 | 国产精品久久久久久久久久久久久久 | 欧美国产精品 | 日韩欧美在线不卡 | 中文字幕国产一区 | 黄一级| 99久久精品国产一区二区三区 | 伊人性伊人情综合网 | 在线欧美日韩 |