我第一次尝试使用angular2测试。
let component: AlertButtonComponent;
let fixture: ComponentFixture<AlertButtonComponent>;
let de:DebugElement;
it('should have an H1 tag of `Alert Button`' ,() => {
console.log('debug element',de)
expect(de.query(By.css('h1')).nativeElement.innerText).toBe('lert Button')
})
我收到错误typeError: Cannot read property 'query' of undefined
de 是undefined
。
请帮忙
答案 0 :(得分:0)
点击此处:https://angular.io/guide/testing - 以角度
进行测试在有角度的网站上检查您需要如下设置,如果您正在测试组件BannerComponent
,则ID不会在您的代码中看到这种设置
let comp: BannerComponent;
let fixture: ComponentFixture<BannerComponent>;
let de: DebugElement;
let el: HTMLElement;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ BannerComponent ], // declare the test component
});
fixture = TestBed.createComponent(BannerComponent);
comp = fixture.componentInstance; // BannerComponent test instance
// query for the title <h1> by CSS element selector
de = fixture.debugElement.query(By.css('h1'));
错误说let de:DebugElement;
未定义意味着您需要初始化它。
let de:DebugElement = new DebugElement();
或者如果您正在使用,则必须传递服务参考。