Angular &引用;错误:Can';t解析警报的所有参数“;在编译测试床时
我正在为运行的Angular应用程序获取一组初始测试,其中一个依赖项有问题。请参见测试规范的代码:Angular &引用;错误:Can';t解析警报的所有参数“;在编译测试床时,angular,unit-testing,typescript,karma-jasmine,Angular,Unit Testing,Typescript,Karma Jasmine,我正在为运行的Angular应用程序获取一组初始测试,其中一个依赖项有问题。请参见测试规范的代码: import { TestBed, async } from '@angular/core/testing'; import { AppComponent } from './app.component'; import { NavbarComponent } from './navbar/navbar.component'; import { RouterModule, Routes } fro
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { NavbarComponent } from './navbar/navbar.component';
import { RouterModule, Routes } from '@angular/router';
import { Alert } from './models/alert.model';
import { AlertsService } from './services/alerts.service';
describe('AppComponent', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent, NavbarComponent
],
imports: [
RouterModule,
],
providers: [
Alert, AlertsService
]
});
TestBed.compileComponents(); //This appears to be what throws the error
});
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!');
}));
});
虽然有很多关于如何修复服务(特别是路由)的讨论,但对于非常简单的类,我没有发现任何东西,比如下面的警报文件:
export class Alert {
public static id: number = 0;
public message: string;
public id: number;
public type: string;
public dismissible: boolean;
public dismissOnTimeout: number;
constructor(config: any) {
this.message = config.message;
this.id = Alert.id && Alert.id++;
this.type = config.type || 'info';
this.dismissible = true;
this.dismissOnTimeout = settings.alertDismissOnTimeout;
}
}
此“模型”文件没有任何方法,仅作为警报的方便定义存在,您可以使用“新警报(警报)”实例化该警报。它由AlertsService使用,到目前为止它似乎还在工作,appComponent的HTML引用了AlertsService和一些实例化Alert对象的方法。到目前为止还不错,但是当我尝试编译AppComponent时,我在标题中得到了错误。我尝试过使用一个简化为绝对最小值的Alert类存根(基本上是一个空构造函数),它也会返回相同的错误
我(可能不正确)的理解是,为了测试套件正常工作,我必须以某种方式将参数传递到alert对象中。我相当肯定,由于其余代码的结构方式以及实际应用程序的运行方式,循环依赖关系不会出现问题。这里的问题是我是否需要传入某种模拟参数,如果不需要,我可以做些什么来正确编译此规范?我可以通过从@angular/core导入无错误模式并将其作为配置测试模块()块中的模式提供来修复我的问题。由于此测试的目的不是处理警报,也不仅仅是“Hello World”类型的初始化,因此实际上注入大多数警报相关代码是不必要的,通过删减内容,我能够编译此版本:
import { DebugElement, NO_ERRORS_SCHEMA } from '@angular/core';
// We need NO_ERRORS_SCHEMA to ignore subcomponents we aren't actually testing, like the alert modules.
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { AlertsService } from './services/alerts.service';
import { AboutComponent } from './about/about.component';
import * as ng2Bootstrap from 'ng2-bootstrap';
describe('AppComponent', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent, AboutComponent
],
imports: [
ng2Bootstrap.Ng2BootstrapModule
],
providers: [
AlertsService,
],
schemas: [NO_ERRORS_SCHEMA]
});
TestBed.compileComponents();
});
it('should create the app', async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
it(`should have 'Application content' as its title`, async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app.title).toEqual('Application content');
}));
});