我正在为运行的Angular应用程序获取一组初始测试,而我遇到了其中一个依赖项的问题。请参阅测试规范的代码:
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!');
}));
});
虽然有很多关于如何在SO上修复服务(以及特定路由)的讨论,但我没有找到任何非常简单的类,比如下面的Alert文件:
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类的存根简化为绝对最小值(基本上是一个空构造函数),并且返回相同的错误。
我的(可能是不正确的)理解是我不得不让参数传递到警报对象以使测试套件正常工作。我很确定这不是循环依赖的问题,因为其余代码的结构方式,实际的应用程序运行没有问题。这里的问题是我是否需要传递某种模拟参数,如果没有,我该怎么做才能使这个规范正确编译?
答案 0 :(得分:0)
我能够通过从@ angular / core导入NO_ERRORS_SCHEMA并将其作为configureTestingModule()块中的模式提供来解决我的问题。由于此测试的目的不是处理警报,或者远远超过" 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');
}));
});