Angular not.toBe(true) 与 toBeFalsy() 中的单元测试
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中有真有假。当某件事为真时,它显然是真或假。当某物为真时,它可能是也可能不是布尔值,但 "cast" 的值是布尔值。
因此在您的情况下,toBeFalsy
表示本机元素不存在或等于 null/undefined。
not.toBe(true)
检查 nativeElement !== true
是否也检查类型和值,当然本机元素值不是 'true' 作为布尔类型。
有关更多信息,请查看:
我正在编写 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中有真有假。当某件事为真时,它显然是真或假。当某物为真时,它可能是也可能不是布尔值,但 "cast" 的值是布尔值。
因此在您的情况下,toBeFalsy
表示本机元素不存在或等于 null/undefined。
not.toBe(true)
检查 nativeElement !== true
是否也检查类型和值,当然本机元素值不是 'true' 作为布尔类型。
有关更多信息,请查看: