我有一个使用angular(typescript)构建的应用程序我正在尝试为app.component设置测试。此组件中有一个路由器插座。我想知道在这种情况下如何正确设置测试。
app.component.spec.ts看起来像这样
import { TestBed, async } from '@angular/core/testing';
import { inject } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { Component, OnInit, Injectable } from '@angular/core';
import { Router, RouterOutlet } from "@angular/router";
import {DriftInfoDisplayComponent} from "./components/drift-info-display/drift-info-display.component";
import {DriftInfoService} from "./services/http-services/drift-info.service";
import {DriftInfo} from "./model/DriftInfo";
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent,
DriftInfoDisplayComponent
],
providers:[
DriftInfoService,
Router,
RouterOutlet
]
}).compileComponents();
}));
it('should create the app', async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
it(`should have as title 'app works!'`, async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app.title).toEqual('app works!');
}));
it('should render title in a h1 tag', async(() => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h1').textContent).toContain('app works!');
}));
});
但是我在运行ng test时失败了
AppComponent应该创建应用程序失败:无法解析所有问题 路由器的参数:(?,?,?,?,?,?,?,?)。
答案 0 :(得分:0)
Router
是一项服务,而不是一个模块。这意味着它具有入门和正常工作的依赖关系,错误消息告诉您缺少这些依赖项。
您应该从Router
数组中删除providers
并将RouterModule
添加到测试模块中的import
。