【问题标题】:TypeError: Cannot read property 'query' of undefined while unit testing Angular 6TypeError:在单元测试Angular 6时无法读取未定义的属性“查询”
【发布时间】:2019-03-10 15:40:02
【问题描述】:

我正在学习使用 karma 和 Jasmine 对 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)

【问题讨论】:

  • 您是否考虑过分配一个值以使其不是未定义?

标签: angular unit-testing karma-jasmine angular6


【解决方案1】:

您的变量de 没有分配任何东西,它需要来自fixture 的debugElement。将de = fixture.debugElement 放入beforeEach(在fixture 定义之后)或it('should have a p tag of dash works!')(在您的期望之前)

【讨论】:

    【解决方案2】:

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

    let de: DebugElement = fixture.debugElement;  // OR de = fixture.debugElement;  after creating component 
    

    【讨论】:

      猜你喜欢
      • 2021-01-22
      • 2020-06-13
      • 2018-12-13
      • 1970-01-01
      • 2020-01-17
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 2019-12-07
      相关资源
      最近更新 更多