Я изучаю юнит-тестирование для Angular 6, используя карму и жасмин.Я пробовал простой модульный тест текста, присутствующего внутри тега <p>
.Вот мой HTML-код:
<p>
dash works!
</p>
А также в моем dash.component.spec.ts я даю такой тестовый код:
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!');
});
});
После выполнения теста ng яя получаю эту ошибку.Пожалуйста, предложите мне, как продолжить.
TypeError: Cannot read property 'query' of undefined
at UserContext.eval (./src/app/dash/dash.component.spec.ts?:32:19)
at ZoneDelegate.invoke (./node_modules/zone.js/dist/zone.js?:387:26)
at ProxyZoneSpec.onInvoke (./node_modules/zone.js/dist/zone-testing.js?:287:39)
at ZoneDelegate.invoke (./node_modules/zone.js/dist/zone.js?:386:32)
at Zone.run (./node_modules/zone.js/dist/zone.js?:137:43)
at runInTestZone (./node_modules/zone.js/dist/zone-testing.js?:508:34)
at UserContext.eval (./node_modules/zone.js/dist/zone-testing.js?:523:20)
at attempt (http://localhost:9876/absoluteD:/thbs/coe/iot/2.POC/Unit%20Testing/angular-unit-testing/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:4478:46)
at ZoneQueueRunner.QueueRunner.run (http://localhost:9876/absoluteD:/thbs/coe/iot/2.POC/Unit%20Testing/angular-unit-testing/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:4402:20)
at runNext (http://localhost:9876/absoluteD:/thbs/coe/iot/2.POC/Unit%20Testing/angular-unit-testing/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:4446:20)