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

強(qiáng)制失敗 Jasmine 測試

Force-failing a Jasmine test(強(qiáng)制失敗 Jasmine 測試)
本文介紹了強(qiáng)制失敗 Jasmine 測試的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

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

如果我在測試中包含永遠(yuǎn)不會(huì)到達(dá)的代碼(例如,promise 序列的 fail 子句),我該如何強(qiáng)制測試失敗?

If I have code in a test that should never be reached (for example the fail clause of a promise sequence), how can I force-fail the test?

我使用類似 expect(true).toBe(false); 之類的東西,但這并不漂亮.

I use something like expect(true).toBe(false); but this is not pretty.

另一種方法是等待測試超時(shí),我想避免這種情況(因?yàn)樗苈?.

The alternative is waiting for the test to timeout, which I want to avoid (because it is slow).

推薦答案

Jasmine 提供了一個(gè)全局方法 fail(),可以在 spec 塊中使用 it()并且還允許使用自定義錯(cuò)誤消息:

Jasmine provides a global method fail(), which can be used inside spec blocks it() and also allows to use custom error message:

it('should finish successfully', function (done) {
  MyService.getNumber()
  .success(function (number) {
    expect(number).toBe(2);
    done();
  })
  .fail(function (err) {
    fail('Unwanted code branch');
  });
});

這是 Jasmine 的內(nèi)置功能,與我在下面提到的錯(cuò)誤"方法相比,它在任何地方都能正常工作.

This is built-in Jasmine functionality and it works fine everywhere in comparison with the 'error' method I've mentioned below.

更新前:

您可以從該代碼分支拋出錯(cuò)誤,它會(huì)立即使規(guī)范失敗,您將能夠提供自定義錯(cuò)誤消息:

You can throw an error from that code branch, it will fail a spec immediately and you'll be able to provide custom error message:

it('should finish successfully', function (done) {
  MyService.getNumber()
  .success(function (number) {
    expect(number).toBe(2);
    done();
  })
  .fail(function (err) {
    throw new Error('Unwanted code branch');
  });
});

但是你應(yīng)該小心,如果你想從 Promise 成功處理程序 then() 拋出一個(gè)錯(cuò)誤,因?yàn)殄e(cuò)誤會(huì)被吞沒在那里并且永遠(yuǎn)不會(huì)出現(xiàn).此外,您應(yīng)該注意應(yīng)用程序中可能存在的錯(cuò)誤處理程序,它們可能會(huì)在您的應(yīng)用程序中捕獲此錯(cuò)誤,因此它不會(huì)導(dǎo)致測試失敗.

But you should be careful, if you want to throw an error from Promise success handler then(), because the error will be swallowed in there and will never come up. Also you should be aware of the possible error handlers in your app, which might catch this error inside your app, so as a result it won't be able to fail a test.

這篇關(guān)于強(qiáng)制失敗 Jasmine 測試的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(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 測試裝置加載?) - IT屋-程序員軟件開發(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(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對(duì)鏈?zhǔn)椒椒ㄟM(jìn)行單元測試)
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ù)調(diào)用?)
主站蜘蛛池模板: 亚洲一区二区三区四区五区中文 | 欧美性生活视频 | 成人欧美一区二区三区黑人孕妇 | 成人免费日韩 | 免费黄篇| 久久久久网站 | 黄色毛片在线观看 | 欧美成人一级 | 一区二区三区四区免费观看 | av毛片在线播放 | 久久久国产一区二区三区四区小说 | 欧美一区在线视频 | 国产精品欧美一区二区 | 亚洲一区二区在线 | 精品视频国产 | 黄色av免费 | 国产乱码一二三区精品 | 日韩视频国产 | 免费高潮视频95在线观看网站 | www.亚洲精品 | 最新日韩在线视频 | 91麻豆蜜桃一区二区三区 | 色综合久久天天综合网 | 免费在线观看成人av | 色毛片 | 国产小视频在线 | 黄网站免费观看 | 色综合久久久久 | h网站在线观看 | 成人在线播放网站 | 黄色成人免费看 | 综合网中文字幕 | 在线国产小视频 | 精品久久精品 | 免费观看成人鲁鲁鲁鲁鲁视频 | 精品欧美一区二区三区久久久 | 久久久蜜桃 | 男女网站视频 | 夜夜爽99久久国产综合精品女不卡 | 一区二区三区四区在线播放 | 麻豆成人在线视频 |