Angular TypeError:无法读取属性';查询';单元测试时的未定义值6

Angular TypeError:无法读取属性';查询';单元测试时的未定义值6,angular,unit-testing,karma-jasmine,angular6,Angular,Unit Testing,Karma Jasmine,Angular6,我正在学习使用karma和Jasmine进行Angular 6的单元测试。我尝试了对标记中的文本进行简单的单元测试。以下是我的HTML代码: <p> dash works! </p> 将变量“de”分配给fixture的debugElement,如下所示: import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { DashComponent } from '

我正在学习使用karma和Jasmine进行Angular 6的单元测试。我尝试了对
标记中的文本进行简单的单元测试。以下是我的HTML代码:

<p>
  dash works!
</p>

将变量“
de
”分配给fixture的debugElement,如下所示:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { DashComponent } from './dash.component';
import { DebugElement } from '@angular/core';
import { By } from '@angular/platform-browser';

describe('DashComponent', () => {
  let component: DashComponent;
  let fixture: ComponentFixture<DashComponent>;
  let de: DebugElement;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ DashComponent ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(DashComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
    it('should have a p tag of `dash works!`', () => {
        expect(de.query(By.css('p')).nativeElement.innerText).toBe('dash works!');
    });
});
let de: DebugElement = fixture.debugElement;  // OR de = fixture.debugElement;  after creating component 

您的变量
de
没有分配任何内容,它需要fixture中的debugElement。将
de=fixture.debugElement
放在每个
之前(在
fixture
定义之后)或
它('should have a p tag of dash works!')
(在您预期之前)

您是否考虑过分配一个值以使其不被定义?
let de: DebugElement = fixture.debugElement;  // OR de = fixture.debugElement;  after creating component