在运行测试用例时,它将引发此错误。 尽管在Angular中运行测试用例时抛出错误,但几乎添加了所有必需的组件和服务以及其他依赖项
Error: StaticInjectorError(DynamicTestModule)[Broadcaster]:
StaticInjectorError(Platform: core)[Broadcaster]:
NullInjectorError: No provider for Broadcaster!
这是规范文件代码
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {HttpClientModule} from '@angular/common/http';
import {RouterModule, Routes} from '@angular/router';
import { FormsModule } from '@angular/forms';
import {APP_BASE_HREF} from '@angular/common';
import {CustomiseMenuComponent} from './customise-menu.component';
describe('CustomiseMenuComponent', () => {
let component: CustomiseMenuComponent;
let fixture: ComponentFixture<CustomiseMenuComponent>;
const appRoutes: Routes = [
{path: 'designs', component: CustomiseMenuComponent}
];
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
CustomiseMenuComponent,
],
imports: [
FormsModule,
HttpClientModule,
RouterModule.forRoot(appRoutes)
],
providers: [
{provide: APP_BASE_HREF, useValue : '/' }
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CustomiseMenuComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
答案 0 :(得分:1)
这可能是关键词:“ 没有广播公司的提供商!”。我想,您应该将Broadcaster添加到“ AppModule”文件中的“ providers”数组中。
@NgModule({
declarations: [
...
],
imports: [
...
],
providers: [
...
Broadcaster,
...
],
bootstrap: [
AppComponent
]
更新:如果您正在使用此服务,也许可以在这里查看文档: https://github.com/ranbuch/ng-broadcaster