Javascript toBe(true) vs toBeTruthy() vs toBeTrue()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32615713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
toBe(true) vs toBeTruthy() vs toBeTrue()
提问by alecxe
What is the difference between expect(something).toBe(true), expect(something).toBeTruthy()and expect(something).toBeTrue()?
之间有什么区别expect(something).toBe(true),expect(something).toBeTruthy()和expect(something).toBeTrue()?
Note that toBeTrue()is a custom matcherintroduced in jasmine-matchersamong other useful and handy matchers like toHaveMethod()or toBeArrayOfStrings().
请注意,这toBeTrue()是在其他有用且方便的匹配器(如或 )中引入的自定义匹配jasmine-matchers器。toHaveMethod()toBeArrayOfStrings()
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?
这个问题是通用的,但是,作为一个现实世界的例子,我正在测试一个元素是否显示在protractor. 在这种情况下我应该使用哪个匹配器?
expect(elm.isDisplayed()).toBe(true);
expect(elm.isDisplayed()).toBeTruthy();
expect(elm.isDisplayed()).toBeTrue();
回答by Louis
What I do when I wonder something like the question asked here is go to the source.
当我想知道这里提出的问题时,我会做的是去源头。
toBe()
成为()
expect().toBe()is defined as:
expect().toBe()定义为:
function toBe() {
return {
compare: function(actual, expected) {
return {
pass: actual === expected
};
}
};
}
It performs its test with ===which means that when used as expect(foo).toBe(true), it will pass only if fooactually has the value true. Truthy values won't make the test pass.
它执行它的测试,===这意味着当使用 as 时expect(foo).toBe(true),它只有在foo实际具有 value时才会通过true。真实值不会使测试通过。
toBeTruthy()
toBeTruthy()
expect().toBeTruthy()is defined as:
function toBeTruthy() {
return {
compare: function(actual) {
return {
pass: !!actual
};
}
};
}
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 expectto a boolean. Note that contrarily to what the currently accepted answer implies, == trueis nota correct test for truthiness. You'll get funny things like
如果将此值强制转换为布尔值产生该值,则该值是真值true。该操作!!通过强制传递给expect布尔值的值来测试真实性。请注意,反之目前公认的答案是什么暗示,== true是不是对感实性正确的测试。你会得到有趣的东西,比如
> "hello" == true
false
> "" == true
false
> [] == true
false
> [1, 2, 3] == true
false
Whereas using !!yields:
而使用!!收益率:
> !!"hello"
true
> !!""
false
> !![1, 2, 3]
true
> !![]
true
(Yes, empty or not, an array is truthy.)
(是的,无论是否为空,数组都是真实的。)
toBeTrue()
是真实的()
expect().toBeTrue()is part of Jasmine-Matchers(which is registered on npm as jasmine-expectafter a later project registered jasmine-matchersfirst).
expect().toBeTrue()是Jasmine-Matchers 的一部分(它在 npm 上注册,因为jasmine-expect后来的项目jasmine-matchers首先注册)。
expect().toBeTrue()is defined as:
function toBeTrue(actual) {
return actual === true ||
is(actual, 'Boolean') &&
actual.valueOf();
}
The difference with expect().toBeTrue()and expect().toBe(true)is that expect().toBeTrue()tests whether it is dealing with a Booleanobject. expect(new Boolean(true)).toBe(true)would fail whereas expect(new Boolean(true)).toBeTrue()would pass. This is because of this funny thing:
与expect().toBeTrue()和的区别expect().toBe(true)在于expect().toBeTrue()测试它是否正在处理一个Boolean对象。expect(new Boolean(true)).toBe(true)会失败而expect(new Boolean(true)).toBeTrue()会通过。这是因为这个有趣的事情:
> new Boolean(true) === true
false
> new Boolean(true) === false
false
At least it is truthy:
至少它是真实的:
> !!new Boolean(true)
true
Which is best suited for use with elem.isDisplayed()?
哪个最适合与elem.isDisplayed()?
Ultimately Protractor hands off this request to Selenium. The documentationstates 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.
最终 Protractor 将这个请求交给 Selenium。该文档指出所产生的值.isDisplayed()是一个承诺解析为一个boolean。我会按面值计算并使用.toBeTrue()or .toBe(true)。如果我发现实现返回真/假值的情况,我会提交错误报告。
回答by micah
In javascript there are trues and truthys. When something is true it is obviously true or false. When something is truthy it may or may not be a boolean, but the "cast" value of is a boolean.
在javascript中有trues和truthys。当某事为真时,它显然是真还是假。当某事物为真值时,它可能是也可能不是布尔值,但它的“强制转换”值是一个布尔值。
Examples.
例子。
true == true; // (true) true
1 == true; // (true) truthy
"hello" == true; // (true) truthy
[1, 2, 3] == true; // (true) truthy
[] == false; // (true) truthy
false == false; // (true) true
0 == false; // (true) truthy
"" == false; // (true) truthy
undefined == false; // (true) truthy
null == false; // (true) truthy
This can make things simpler if you want to check if a string is set or an array has any values.
如果您想检查字符串是否已设置或数组是否有任何值,这可以使事情变得更简单。
var users = [];
if(users) {
// this array is populated. do something with the array
}
var name = "";
if(!name) {
// you forgot to enter your name!
}
And as stated. expect(something).toBe(true)and expect(something).toBeTrue()is the same. But expect(something).toBeTruthy()is not the same as either of those.
并如所述。expect(something).toBe(true)并且expect(something).toBeTrue()是一样的。但expect(something).toBeTruthy()与其中任何一个都不相同。
回答by Ismael Miguel
Disclamer: This is just a wild guess
免责声明:这只是一个疯狂的猜测
I know everybody loves an easy-to-read list:
我知道每个人都喜欢一个易于阅读的列表:
toBe(<value>)- The returned value is the same as<value>toBeTrue()- Checks if the returned value istruetoBeTruthy()- Check if the value, when cast to a boolean, will be a truthy valueTruthy values are all values that aren't
0,''(empty string),false,null,NaN,undefinedor[](empty array)*.* Notice that when you run
!![], it returnstrue, but when you run[] == falseit also returnstrue. It depends on how it is implemented. In other words:(!![]) === ([] == false)
toBe(<value>)- 返回值与<value>toBeTrue()- 检查返回值是否为truetoBeTruthy()- 检查该值在转换为布尔值时是否为真值真值是所有不是
0、''(空字符串)false、null、NaN、undefined或[](空数组)* 的值。* 请注意,当您运行时
!![],它会返回true,但是当您运行时,[] == false它也会返回true。这取决于它是如何实现的。换句话说:(!![]) === ([] == false)
On your example, toBe(true)and toBeTrue()will yield the same results.
在你的榜样,toBe(true)并toBeTrue()会产生相同的结果。
回答by Girish Sortur
There are a lot many good answers out there, i just wanted to add a scenario where the usage of these expectations might be helpful. Using element.all(xxx), if i need to check if all elements are displayed at a single run, i can perform -
有很多很好的答案,我只是想添加一个场景,其中使用这些期望可能会有所帮助。使用element.all(xxx),如果我需要检查所有元素是否在一次运行中显示,我可以执行 -
expect(element.all(xxx).isDisplayed()).toBeTruthy(); //Expectation passes
expect(element.all(xxx).isDisplayed()).toBe(true); //Expectation fails
expect(element.all(xxx).isDisplayed()).toBeTrue(); //Expectation fails
Reason being .all()returns an array of values and so all kinds of expectations(getText, isPresent, etc...) can be performed with toBeTruthy()when .all()comes into picture. Hope this helps.
原因是.all()返回值的阵列等各种预期(getText,isPresent等...)可以与执行toBeTruthy()时.all()进入图片。希望这可以帮助。

