【发布时间】:2021-05-19 02:29:56
【问题描述】:
在 Angular 中运行 ng 测试时,我收到错误消息 SidebarComponent > 应该创建 NullInjectorError: R3InjectorError(DynamicTestModule)[AuthserviceService -> HttpClient -> HttpClient]: NullInjectorError: 没有 HttpClient 的提供者!
sidebar.component.spec.ts
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { SidebarComponent } from './sidebar.component';
describe('SidebarComponent', () => {
let component: SidebarComponent;
let fixture: ComponentFixture<SidebarComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ SidebarComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SidebarComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { NgModule ,Input, Output, EventEmitter} from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ChartsModule, ThemeService } from 'ng2-charts';
import { AuthserviceService } from './authservice.service';
import { DashboardService } from './dashboard.service';
import { AppComponent } from './app.component';
import { NavbarComponent } from './shared/navbar/navbar.component';
import { SidebarComponent } from './shared/sidebar/sidebar.component';
import { FooterComponent } from './shared/footer/footer.component';
import { DashboardComponent } from './main/dashboard/dashboard.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { SpinnerComponent } from './shared/spinner/spinner.component';
import { AboutComponent } from './main/about/about.component';
import { NotificationsComponent } from './main/notifications/notifications.component';
import { LoginComponent } from './login/login.component';
import { MainComponent } from './main/main.component';
import { UserinfoComponent } from './userinfo/userinfo.component';
import { ReportComponent } from './main/report/report.component';
import { Shared } from './shared.service';
@NgModule({
declarations: [
AppComponent,
NavbarComponent,
SidebarComponent,
FooterComponent,
DashboardComponent,
SpinnerComponent,
AboutComponent,
NotificationsComponent,
LoginComponent,
MainComponent,
UserinfoComponent,
ReportComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
NgbModule,
BrowserAnimationsModule,
FormsModule,
ReactiveFormsModule,
ChartsModule,
HttpClientModule,
],
providers: [ThemeService,AuthserviceService,DashboardService,Shared],
bootstrap: [AppComponent]
})
export class AppModule { }
我是这个测试的新手,任何人都可以帮助我。
【问题讨论】:
标签: angular karma-jasmine karma-runner e2e-testing angular-test