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

时间:2023-04-30
本文介绍了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模板网!

上一篇:如何读取参数的值,存在于规格中量角器的 con 下一篇:用于 Rails 3.1+ 的 Jasmine 与 Mocha JavaScript 测试

相关文章

最新文章