toBe (true) vs toBeTruthy () vs toBeTrue ()
무엇 사이의 차이 expect(something).toBe(true)
, expect(something).toBeTruthy()
그리고 expect(something).toBeTrue()
?
참고 toBeTrue()
A는 사용자 정의 정규 도입 jasmine-matchers
과 같은 다른 유용하고 편리한 정합 기 중 toHaveMethod()
또는 toBeArrayOfStrings()
.
이 질문은 일반적인 것이지만 실제 예를 들어 요소가에 표시되는지 테스트하고 protractor
있습니다. 이 경우 어떤 매처를 사용해야합니까?
expect(elm.isDisplayed()).toBe(true);
expect(elm.isDisplayed()).toBeTruthy();
expect(elm.isDisplayed()).toBeTrue();
여기서 묻는 질문과 같은 궁금한 점이 있으면 소스로 이동하십시오.
되려고()
expect().toBe()
다음과 같이 정의됩니다.
function toBe() {
return {
compare: function(actual, expected) {
return {
pass: actual === expected
};
}
};
}
테스트는 다음과 ===
같이 사용될 때 실제로 값이있는 expect(foo).toBe(true)
경우에만 통과 함을 의미합니다 . 진실한 가치는 시험을 통과시키지 못합니다.foo
true
toBeTruthy ()
expect().toBeTruthy()
다음과 같이 정의됩니다.
function toBeTruthy() {
return {
compare: function(actual) {
return {
pass: !!actual
};
}
};
}
강제 변환
이 값을 부울로 강제 변환하면 값이 생성되면 값이 진실 true
입니다. 이 작업 은 부울에 !!
전달 된 값을 강제 적용하여 진실성을 테스트합니다 expect
. 반대로 현재 허용 대답은 무엇에주의 의미 , == true
입니다 하지 truthiness에 대한 정확한 테스트. 당신은 같은 재미있는 것들을 얻을 것이다
> "hello" == true
false
> "" == true
false
> [] == true
false
> [1, 2, 3] == true
false
!!
수율을 사용하는 반면 :
> !!"hello"
true
> !!""
false
> !![1, 2, 3]
true
> !![]
true
(예, 비어 있든 아니든 배열은 진실입니다.)
toBeTrue ()
expect().toBeTrue()
Jasmine-Matchers의 일부입니다 ( jasmine-expect
나중에 프로젝트를 jasmine-matchers
먼저 등록한 후 npm에 등록됨 ).
expect().toBeTrue()
다음과 같이 정의됩니다.
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 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
At least it is truthy:
> !!new Boolean(true)
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.
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.
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.
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 istrue
toBeTruthy()
- 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
,undefined
or[]
(empty array)*.* Notice that when you run
!![]
, it returnstrue
, but when you run[] == false
it also returnstrue
. It depends on how it is implemented. In other words:(!![]) === ([] == false)
On your example, toBe(true)
and toBeTrue()
will yield the same results.
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 -
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.
참고URL : https://stackoverflow.com/questions/32615713/tobetrue-vs-tobetruthy-vs-tobetrue
'IT story' 카테고리의 다른 글
현재 모듈에서 setattr ()을 어떻게 호출합니까? (0) | 2020.07.05 |
---|---|
NaN이 포함 된 Pandas 열을 dtype`int`로 변환 (0) | 2020.07.05 |
하이퍼 링크에서 모든 스타일링 / 포맷 제거 (0) | 2020.07.05 |
Mockito를 사용할 때 조롱과 스파이의 차이점은 무엇입니까? (0) | 2020.07.05 |
MySQL에서 OFFSET과 함께 LIMIT를 사용할 때 어떤 행이 반환됩니까? (0) | 2020.07.05 |