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

toBe(true) vs toBeTruthy() vs toBeTrue()

toBe(true) vs toBeTruthy() vs toBeTrue()(toBe(true) vs toBeTruthy() vs toBeTrue())
本文介紹了toBe(true) vs toBeTruthy() vs toBeTrue()的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

expect(something).toBe(true)expect(something).toBeTruthy()expect(something).toBeTrue有什么區別()?

請注意,toBeTrue() 中引入的自定義匹配器>jasmine-matchers 以及其他有用且方便的匹配器,例如 toHaveMethod()toBeArrayOfStrings().

Note that toBeTrue() is a custom matcher introduced in jasmine-matchers among other useful and handy matchers like toHaveMethod() or toBeArrayOfStrings().

這個問題是通用的,但是,作為一個真實的例子,我正在測試一個元素是否顯示在 protractor 中.在這種情況下我應該使用哪個匹配器?

The question is meant to be generic, but, as a real-world example, I'm testing that an element is displayed in protractor. Which matcher should I use in this case?

expect(elm.isDisplayed()).toBe(true);
expect(elm.isDisplayed()).toBeTruthy();
expect(elm.isDisplayed()).toBeTrue();

推薦答案

當我想知道類似這里提出的問題時,我會做的是去源頭.

What I do when I wonder something like the question asked here is go to the source.

expect().toBe() 定義為:

function toBe() {
  return {
    compare: function(actual, expected) {
      return {
        pass: actual === expected
      };
    }
  };
}

它使用 === 執行測試,這意味著當用作 expect(foo).toBe(true) 時,只有在 foo 實際上的值為 true.真實值不會使測試通過.

It performs its test with === which means that when used as expect(foo).toBe(true), it will pass only if foo actually has the value true. Truthy values won't make the test pass.

expect().toBeTruthy() 定義為:

function toBeTruthy() {
  return {
    compare: function(actual) {
      return {
        pass: !!actual
      };
    }
  };
}

類型強制

如果將該值強制轉換為布爾值,則該值是真值 true.!! 操作通過將傳遞給 expect 的值強制為布爾值來測試真實性.請注意,與當前接受的答案暗示相反,== true 不是 真實性的正確測試.你會得到一些有趣的東西,比如

Type coercion

A value is truthy if the coercion of this value to a boolean yields the value true. The operation !! tests for truthiness by coercing the value passed to expect to a boolean. Note that contrarily to what the currently accepted answer implies, == true is not a correct test for truthiness. You'll get funny things like

> "hello" == true
false
> "" == true
false
> [] == true
false
> [1, 2, 3] == true
false

而使用 !! 會產生:

> !!"hello"
true
> !!""
false
> !![1, 2, 3]
true
> !![] 
true

(是的,無論是否為空,數組都是真實的.)

(Yes, empty or not, an array is truthy.)

expect().toBeTrue() 是 Jasmine-Matchers 的一部分a> (在后來的項目首先注冊 jasmine-matchers 后,在 npm 上注冊為 jasmine-expect).

expect().toBeTrue() is part of Jasmine-Matchers (which is registered on npm as jasmine-expect after a later project registered jasmine-matchers first).

expect().toBeTrue() 定義為:

function toBeTrue(actual) {
  return actual === true ||
    is(actual, 'Boolean') &&
    actual.valueOf();
}

expect().toBeTrue()expect().toBe(true)的區別在于expect().toBeTrue()code> 測試它是否在處理 Boolean 對象.expect(new Boolean(true)).toBe(true) 會失敗,而 expect(new Boolean(true)).toBeTrue() 會通過.這是因為這個有趣的事情:

The difference with expect().toBeTrue() and expect().toBe(true) is that expect().toBeTrue() tests whether it is dealing with a Boolean object. expect(new Boolean(true)).toBe(true) would fail whereas expect(new Boolean(true)).toBeTrue() would pass. This is because of this funny thing:

> new Boolean(true) === true
false
> new Boolean(true) === false
false

至少它是真實的:

> !!new Boolean(true)
true

哪個最適合與 elem.isDisplayed() 一起使用?

最終 Protractor 將此請求交給 Selenium.documentation 聲明 產生的值.isDisplayed() 是一個解析為 boolean 的承諾.我會從表面上看它并使用 .toBeTrue().toBe(true).如果我發現實現返回真值/假值的情況,我會提交錯誤報告.

Which is best suited for use with elem.isDisplayed()?

Ultimately Protractor hands off this request to Selenium. The documentation states that the value produced by .isDisplayed() is a promise that resolves to a boolean. I would take it at face value and use .toBeTrue() or .toBe(true). If I found a case where the implementation returns truthy/falsy values, I would file a bug report.

這篇關于toBe(true) vs toBeTruthy() vs toBeTrue()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 精品久久久久久久 | 日本不卡一区 | 很黄很污的网站 | 日韩色图视频 | 久久精品国产v日韩v亚洲 | 欧美a v在线 | 日韩喷潮| 国产一区在线免费观看 | 日日干日日色 | 一区二区日韩 | 久久久男人的天堂 | 日韩中文字幕在线视频观看 | 精品国产乱码久久久久久图片 | 一区二区三区国产在线观看 | 亚洲一区 | 亚洲精品一区二区三区免 | 综合二区 | 国产伦精品一区二区三区照片91 | 一区二区av| 久久午夜视频 | 国产精品久久久久久久久久妞妞 | 国产精品久久久久久久久久 | 95国产精品 | www.4虎影院| 爱爱视频在线观看 | 午夜91| 日日操夜夜操视频 | 日韩免费在线观看视频 | 成人欧美一区二区三区白人 | 国产精品美女久久久久aⅴ国产馆 | 欧美日韩最新 | 亚洲在线视频 | 天天精品综合 | 精品欧美一区二区三区 | 精品毛片| 男女精品网站 | 久久精品欧美一区二区三区麻豆 | 国产69精品久久99不卡免费版 | 日韩一级免费看 | 国产农村一级片 | 欧美一区二区三区 |