Angular 非角度的单元测试。toBe(真)与toBeFalsy()

Angular 非角度的单元测试。toBe(真)与toBeFalsy(),angular,jasmine,protractor,karma-jasmine,Angular,Jasmine,Protractor,Karma Jasmine,我正在写一个角度单元测试,我面临一个错误。 如果我写toBeFalsy(),测试就会失败。 但是如果我不写,那么测试就可以了 有人能帮我吗 let fixture: ComponentFixture<RootComponent>; let debugElement: DebugElement; beforeEach(async () => { fixture = TestBed.createComponent(RootComponent); await exe

我正在写一个角度单元测试,我面临一个错误。 如果我写toBeFalsy(),测试就会失败。 但是如果我不写,那么测试就可以了

有人能帮我吗

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();
  });
let夹具:组件夹具;
let-debugElement:debugElement;
beforeach(异步()=>{
fixture=TestBed.createComponent(RootComponent);
等待执行更改检测(夹具);
组件=fixture.componentInstance;
debugElement=fixture.debugElement.query(By.css(“#overlay”);
});
它('应该隐藏覆盖并重置超时',()=>{
const appState=new ApplicationStateService(如有则为null);
appState.timeout=false;
component.isTimeoutOverlayVisible=appState.timeout;
fixture.detectChanges();
expect(debugElement.nativeElement).toBeFalsy();
});
toBeFalsy()测试将失败,但消息会传来。
应为。。。做一个虚伪的人。


不是。toBe(true)测试将起作用。

在javascript中有trues和truthys。当某事是真的时,它显然是真的或假的。当某事物是真实的时,它可能是布尔值,也可能不是布尔值,但的“强制转换”值是布尔值

因此,在您的例子中,
toBeFalsy
意味着本机元素不存在或等于null/undefined

not.toBe(true)
检查
nativeElement!==true
,它还检查类型和值,当然本机元素值不是布尔类型的“true”

有关更多信息,请查看: