我正在编写一个 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 中,有 true 和 trues。当某件事是真的时,它显然是真的或假的。当某事为真时,它可能是也可能不是 bool 值,但“转换”值是 bool 值。
因此,在您的情况下,toBeFalsy
意味着 native 元素不存在或等于 null/未定义。
not.toBe(true)
检查 nativeElement !== true
是否也检查类型和值,当然 native 元素值不是 ' true' 作为 bool 类型。
欲了解更多信息,请查看:toBe(true) vs toBeTruthy() vs toBeTrue()
关于angular - Angular 中的单元测试 not.toBe(true) 与 toBeFalsy(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59632008/