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

Jasmine.js 測試 - 窺探 window.open

Jasmine.js Testing - spy on window.open(Jasmine.js 測試 - 窺探 window.open)
本文介紹了Jasmine.js 測試 - 窺探 window.open的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

JS

var link = this.notificationDiv.getElementsByTagName('a')[0];

link.addEventListener('click', function (evt){
    evt.preventDefault();
    visitDestination(next);
  }, false);
}

var visitDestination = function(next){
    window.open(next)
}

規(guī)格

  var next = "http://www.example.com"

  it( 'should test window open event', function() {

    var spyEvent = spyOnEvent('#link', 'click' ).andCallFake(visitDestination(next));;
    $('#link')[0].click();
    expect( 'click' ).toHaveBeenTriggeredOn( '#link' );
    expect( spyEvent ).toHaveBeenTriggered();

    expect(window.open).toBeDefined();
    expect(window.open).toBe('http://www.example.com');    
 });

如何編寫規(guī)范以測試何時單擊鏈接它調用 visitDestination 并確保 window.open == next?當我嘗試運行規(guī)范時,它會打開新窗口.

How to write the spec to test for when link is clicked it calls visitDestination and to ensures window.open == next? When I try to run the spec it opens the new window.

推薦答案

所以,window.open 是瀏覽器提供的一個方法.我不相信它會重置自身的價值.所以這個:

So, window.open is a method provided by the browser. I don't believe it resets the value of itself. So this:

expect(window.open).toBe('http://www.example.com');  

...無論如何都會失敗.

... is going to fail no matter what.

你想要的是創(chuàng)建一個 window.open 方法的模擬:

What you want is to create a mock of the window.open method:

spyOn(window, 'open')

這將允許您跟蹤它的運行時間.它還將阻止實際的 window.open 函數(shù)運行.因此,運行測試時不會打開新窗口.

This will allow you to track when it has been run. It will also prevent the actual window.open function from running. So a new window will not open when you run the test.

接下來您應該測試 window.open 方法是否已運行:

Next you should test that the window.open method was run:

expect(window.open).toHaveBeenCalledWith(next)

更多細節(jié).如果您想測試是否已運行 visitDestination,那么您可以:

More detail. If you want to test that visitDestination has been run then you would do:

spyOn(window, 'visitDestination').and.callThrough()

...

expect(window.visitDestination).toHaveBeenCalled()

.and.callThrough() 在這里非常重要.如果您不使用它,那么普通的 visitDestination 將被替換為不執(zhí)行任何操作的虛擬/模擬函數(shù).

The .and.callThrough() is really important here. If you don't use it then the normal visitDestination will be replace with a dummy/mock function which does nothing.

這篇關于Jasmine.js 測試 - 窺探 window.open的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發(fā)技術
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 - 如何監(jiān)視函數(shù)中的函數(shù)調用?)
主站蜘蛛池模板: 午夜丁香视频在线观看 | 一区二区久久 | 日韩免费1区二区电影 | 精品国产乱码久久久久久丨区2区 | 中文区中文字幕免费看 | 国产精品一区二区三区在线 | 国产精品精品视频一区二区三区 | 国产一区二区三区在线看 | 99中文字幕 | 成年男女免费视频网站 | 91精品国产综合久久久久蜜臀 | 巨大黑人极品videos精品 | 亚洲精品一区二区在线 | 91精品一区二区三区久久久久 | 成人欧美一区二区三区黑人孕妇 | 久久综合国产精品 | 成人激情视频免费在线观看 | 国产一区2区 | 国产精品久久久久久 | 一区二区三区国产 | 美女网站视频免费黄 | 91av视频在线 | av一区二区三区 | 国产黄色在线观看 | 国产区在线免费观看 | 99re热这里只有精品视频 | 美女拍拍拍网站 | 免费观看www | 国产精品欧美一区喷水 | 欧美一级片黄色 | av资源中文在线天堂 | 国产激情在线观看视频 | 亚洲成人精品一区二区 | 国产精品久久久久久久久久久久冷 | 精品国产一区二区三区在线观看 | 国产999精品久久久 精品三级在线观看 | 国产亚洲精品久久久优势 | 颜色网站在线观看 | 久久不卡| 在线一区| 91精品一区二区三区久久久久 |