ale*_*cxe 133 javascript testing jasmine protractor jasmine-matchers
有什么区别expect(something).toBe(true)
,expect(something).toBeTruthy()
和expect(something).toBeTrue()
?
请注意,这toBeTrue()
是一个自定义匹配器,介绍在jasmine-matchers
其他有用和方便的匹配器,如toHaveMethod()
或toBeArrayOfStrings()
.
问题是通用的,但是,作为一个真实的例子,我正在测试一个元素是否显示在protractor
.在这种情况下我应该使用哪个匹配器?
expect(elm.isDisplayed()).toBe(true);
expect(elm.isDisplayed()).toBeTruthy();
expect(elm.isDisplayed()).toBeTrue();
Run Code Online (Sandbox Code Playgroud)
Lou*_*uis 190
当我想知道像这里提出的问题之类的东西时,我会做什么.
expect().toBe()
定义为:
function toBe() {
return {
compare: function(actual, expected) {
return {
pass: actual === expected
};
}
};
}
Run Code Online (Sandbox Code Playgroud)
它执行测试,===
这意味着当用作时expect(foo).toBe(true)
,它只有在foo
实际具有值时才会通过true
.Truthy值不会使测试通过.
function toBeTruthy() {
return {
compare: function(actual) {
return {
pass: !!actual
};
}
};
}
Run Code Online (Sandbox Code Playgroud)
如果将此值强制为布尔值,则该值是真实的true
.该操作!!
通过强制传递给expect
布尔值的值来测试真实性.请注意,反之目前公认的答案是什么暗示,== true
是不是对感实性正确的测试.你会得到有趣的东西
> "hello" == true
false
> "" == true
false
> [] == true
false
> [1, 2, 3] == true
false
Run Code Online (Sandbox Code Playgroud)
而使用!!
产量:
> !!"hello"
true
> !!""
false
> !![1, 2, 3]
true
> !![]
true
Run Code Online (Sandbox Code Playgroud)
(是的,空的与否,数组是真的.)
expect().toBeTrue()
是Jasmine-Matchers的一部分(在jasmine-expect
之后的项目jasmine-matchers
首先注册后,在npm 注册).
expect().toBeTrue()
定义为:
function toBeTrue(actual) {
return actual === true ||
is(actual, 'Boolean') &&
actual.valueOf();
}
Run Code Online (Sandbox Code Playgroud)
与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
Run Code Online (Sandbox Code Playgroud)
至少它是真的:
> !!new Boolean(true)
true
Run Code Online (Sandbox Code Playgroud)
elem.isDisplayed()
?最终,量角器将此请求交给了Selenium.该文档指出所产生的值.isDisplayed()
是解析为一个承诺boolean
.我会采取面值并使用.toBeTrue()
或.toBe(true)
.如果我发现实现返回truthy/falsy值的情况,我会提交错误报告.
mic*_*cah 17
在javascript中有真实和真实.当事情成真时,显然是真或假.当某些东西是真实的时,它可能是也可能不是布尔值,但"cast"值是一个布尔值.
例子.
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
Run Code Online (Sandbox Code Playgroud)
如果要检查字符串是否设置或数组是否包含任何值,这可以使事情变得更简单.
var users = [];
if(users) {
// this array is populated. do something with the array
}
var name = "";
if(!name) {
// you forgot to enter your name!
}
Run Code Online (Sandbox Code Playgroud)
如上所述.expect(something).toBe(true)
并且expect(something).toBeTrue()
是一样的.但expect(something).toBeTruthy()
与其中任何一个都不一样.
Ism*_*uel 11
免责声明:这只是一个疯狂的猜测
我知道每个人都喜欢一个易于阅读的清单:
toBe(<value>)
- 返回值与 <value>
toBeTrue()
- 检查返回的值是否为 true
toBeTruthy()
- 检查当转换为布尔值时,该值是否为真值
Truthy值是不是所有的值0
,''
(空字符串), ,false
,,null
或(空数组)*.NaN
undefined
[]
*请注意,当您运行时!![]
,它会返回true
,但是当您运行[] == false
它时也会返回true
.这取决于它是如何实现的.换一种说法:(!![]) === ([] == false)
在你的榜样,toBe(true)
并toBeTrue()
会产生相同的结果.
当您阅读下面的示例时,请记住这种差异
true === true // true
"string" === true // false
1 === true // false
{} === true // false
Run Code Online (Sandbox Code Playgroud)
但
Boolean("string") === true // true
Boolean(1) === true // true
Boolean({}) === true // true
Run Code Online (Sandbox Code Playgroud)
当传递给的语句expect()
计算结果为时,断言通过true
expect(true).toBe(true) // pass
expect("123" === "123").toBe(true) // pass
Run Code Online (Sandbox Code Playgroud)
在所有其他情况下都会失败
expect("string").toBe(true) // fail
expect(1).toBe(true); // fail
expect({}).toBe(true) // fail
Run Code Online (Sandbox Code Playgroud)
即使所有这些语句true
在执行时都会评估为Boolean()
:
所以你可以将其视为“严格”比较
这个与 的比较类型完全相同,但最近在 Jasmine 中于2019 年 9 月 20 日的.toBe(true)
版本中引入3.5.0
toBeTruthy
另一方面,首先将语句的输出计算为布尔值,然后进行比较
expect(false).toBeTruthy() // fail
expect(null).toBeTruthy() // fail
expect(undefined).toBeTruthy() // fail
expect(NaN).toBeTruthy() // fail
expect("").toBeTruthy() // fail
expect(0).toBeTruthy() // fail
Run Code Online (Sandbox Code Playgroud)
在所有其他情况下它都会通过,例如
expect("string").toBeTruthy() // pass
expect(1).toBeTruthy() // pass
expect({}).toBeTruthy() // pass
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
74943 次 |
最近记录: |