問題描述
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模板網!