[英]Unit Testing in Angular not.toBe(true) vs toBeFalsy()
我正在编写一个 Angular 单元测试,但遇到了一个错误。 如果我写 toBeFalsy() 测试将失败。 但是如果我写 not.toBe(true) 测试将工作。
可能有人帮助我。
let fixture: ComponentFixture<RootComponent>;
let debugElement: DebugElement;
beforeEach(async () => {
fixture = TestBed.createComponent(RootComponent);
await executeChangeDetection(fixture);
component = fixture.componentInstance;
debugElement = fixture.debugElement.query(By.css('#overlay'));
});
it('should hide overlay and reset timeout', () => {
const appState = new ApplicationStateService(null as any);
appState.timeout = false;
component.isTimeoutOverlayVisible = appState.timeout;
fixture.detectChanges();
expect(debugElement.nativeElement).toBeFalsy();
});
toBeFalsy()测试将失败,但随着消息的出现。
Expected <overlay id="overlay">...</overlay> to be falsy.
not.toBe(true)测试将起作用。
在javascript中有trues和truthys。 当某事为真时,它显然是真还是假。 当某事物为真值时,它可能是也可能不是布尔值,但它的“强制转换”值是一个布尔值。
所以在你的情况下, toBeFalsy
意味着本机元素不存在或等于空/未定义。
not.toBe(true)
检查是否nativeElement !== true
它还检查类型和值,当然本机元素值不是作为布尔类型的“真”。
有关更多信息,请查看: toBe(true) vs toBeTruthy() vs toBeTrue()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.