debugElement在我的angular5测试中返回null

时间:2018-01-26 15:46:53

标签: unit-testing angular5 testbed

我希望使用主题组件测试anguar5组件,如in angular.io doc所述。

但我的测试因

而继续失败
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');
    });
});

1 个答案:

答案 0 :(得分:0)

编写测试时,您只需测试组件/服务/管道/指令等,而无需测试其依赖项。

根据上面的代码,我假设DropDownListComponent具有DI依赖性,而providers的{​​{1}}中没有声明DI依赖性,这会引起问题。无论如何,它应该是模拟的,而不是真实的组件。

如果您要测试TestBed-请共享其代码。在不了解其界面的情况下,很难猜测如何为其编写测试。

您可以使用ng-mocks对其进行模拟,然后只需测试DropDownListComponent[valuesList]的值是否正确。

要正确地编译所有组件,您还需要处理[selectedValue]的承诺。

compileComponents

利润。