debugElement在我的angular5测试中返回null。

问题描述 投票:2回答:1

我希望使用一个主机组件来测试一个Anguar5组件,如下所述。在angular.io文档中.

但我的测试一直失败,因为

TypeError: Cannot read property 'query' of null
    at UserContext.<anonymous> (http://localhost:9876/base/config-webpack/spec-bundle.js:293832:39)
    at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/base/config-webpack/spec-bundle.js:288418:26)
    at ProxyZoneSpec../node_modules/zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke (http://localhost:9876/base/config-webpack/spec-bundle.js:287920:39)
    at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/base/config-webpack/spec-bundle.js:288417:32)
    at Zone../node_modules/zone.js/dist/zone.js.Zone.run (http://localhost:9876/base/config-webpack/spec-bundle.js:288168:43)
    at UserContext.<anonymous> (http://localhost:9876/base/config-webpack/spec-bundle.js:287799:34)
    at attempt (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4289:46)
    at ZoneQueueRunner.QueueRunner.run (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4217:20)
    at ZoneQueueRunner.QueueRunner.execute (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4199:10)
    at ZoneQueueRunner../node_modules/zone.js/dist/jasmine-patch.js.jasmine.QueueRunner.ZoneQueueRunner.execute (http://localhost:9876/base/config-webpack/spec-bundle.js:287827:42)

的确,当我记录 fixture.debugElement 时,它返回了 null。

我的测试代码是:

import {} from 'jasmine';

import { Component } from '@angular/core';
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { By }              from '@angular/platform-browser';
import { DropDownListComponent } from './dropDownList.component';

@Component({
    template: '<dropdown-list [valuesList]="valuesList" [selectedValue]="valuesSelected" ></dropdown-list>'
})
class TestComponent {
    valuesList = [{label: 'test_select', value:'test'}, {label: 'test_select2', value:'test2'}];
    valuesSelected = {label: 'test_select', value:'test'};
}

describe('DropDownListComponent', () => {
    let fixture, component;

    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [TestComponent,  DropDownListComponent]
        }).compileComponents();
    });

    it('should display selectedValue', () => {
        fixture = TestBed.createComponent(TestComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
        console.log(fixture.isStable());
        console.log(fixture);
        console.log(component);
        console.log(fixture.debugElement);
        //const fixture = TestBed.createComponent(TestComponent);
        let de = fixture.debugElement.query(By.css(".value-container"));
        let el = de.nativeElement;
        expect(el.textContent).toContain('test_select');
    });
});
unit-testing angular5 testbed
1个回答
0
投票

当你写一个测试时,你只需要测试你的组件服务管道指令等,而不是它的依赖关系。

从上面的代码来看,我假设 DropDownListComponent 中没有声明的DI依赖关系。providersTestBed 而导致了这个问题。总之在这种情况下,它应该是一个mock,而不是一个真正的组件。

如果你想测试 DropDownListComponent - 那么请分享它的代码。如果不了解它的界面,很难猜测如何为它写测试。

你可以使用 ng-mocks 来模拟它,然后你只需要测试那个 [valuesList][selectedValue] 得到正确的值。

同时,为了正确编译所有组件,你需要处理好以下问题 compileComponents'的承诺。

describe('TestComponent', () => {
    beforeEach(() => {
        return TestBed.configureTestingModule({
            declarations: [TestComponent,  MockComponent(DropDownListComponent)],
        }).compileComponents();
    });

    it('should display selectedValue', () => {
        const fixture = MockRender(TestComponent);
        const dropdown = MockHelper.findOrFail(fixture.debugElement, DropDownListComponent);
        expect(dropdown.valuesList).toEqual(fixture.point.valuesList);
        expect(dropdown.selectedValue).toEqual(fixture.point.valuesSelected);
    });
});

盈利。

© www.soinside.com 2019 - 2024. All rights reserved.