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
不是 真实性的正确测试.你会得到一些有趣的东西,比如
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)
.如果我发现实现返回真值/假值的情况,我会提交错误报告.
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模板网!