我在测试我的一个组件时遇到了一些问题。测试如下:
describe('SmpEventsNewCompactEventComponent', () => {
const specService: SmpSpecService = new SmpSpecService();
describe('Component rendering', () => {
let componentInstance: any;
let componentFixture: ComponentFixture<any>;
let cssSelector: string;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
MaterialModule.forRoot()
],
declarations: [
SmpEventsCompactEventComponent,
SmpEventsAddressComponent
],
providers: [
{ provide: SMP_OT_HELPER, useValue: newOtHelperInstance() },
DatePipe,
SmpEventTypeCheckerService,
SmpLangService
]
});
componentFixture = TestBed.createComponent(SmpEventsCompactEventComponent);
componentInstance = componentFixture.componentInstance;
});
describe('Global', () => {
it('GIVEN there are date/time AND address ' +
'WHEN component loads THEN display event', () => {
//// TEST ONE
componentInstance.isEventEnabled = true;
cssSelector = '.events-compact-event';
let debugElement = specService.queryDebugElement(
componentFixture, cssSelector);
expect(debugElement).not.toBeNull();
});
it('GIVEN there are no date/time AND no address ' +
'WHEN component loads THEN do not display event', () => {
//// TEST TWO
componentInstance.isEventEnabled = true;
//componentInstance.isAddressEnabled = false;
// componentInstance.isDateEnabled = false;
cssSelector = '.events-compact-event';
let debugElement = specService.queryDebugElement(
componentFixture, cssSelector);
console.log('--------------', debugElement.nativeElement);
// expect(debugElement.nativeElement).toBe(null);
// expect(true).toBe(true);
expect(debugElement).not.toBeNull();
});
});
});
specService.queryDebugElement的使用方法如下:
queryDebugElement(fixture: ComponentFixture<any>, cssSelector: string): DebugElement {
fixture.detectChanges();
return fixture.debugElement.query(By.css(cssSelector));
}
Whenever i use the
expect(true).toBe(true);
或者
expect(debugElement.nativeElement).toBe(null);
或者
expect(debugElement).not.toBeNull();
在测试二中,测试通常可以正常工作或失败,但每当我使用标签时,会出现问题。
expect(debugElement).toBeNull();
Karma使用的浏览器(已在Chrome / Chromium / PhantomJs中测试)崩溃并且没有给出任何提示。这不是简单的"期望false为true"失败,测试环境完全崩溃。
您看到了什么可以解释这种行为的东西吗?