I am using angular7 and doing unit testing in jasmine and karma. And I am facing error - My packages versions are -Testing - Can't resolve all parameters for (ClassName)import { inject } from '@angular/core/testing';import { MockBackend, MockConnection } from '@angular/http/testing';import { Http, HttpModule, XHRBackend, ResponseOptions, Response, BaseRequestOptions} from '@angular/http';import { NgModule, CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA} from '@angular/core';;fdescribe('ProjectManagementComponent', () => { let comp: ProjectManagementComponent; let fixture: ComponentFixture<ProjectManagementComponent>; let de: DebugElement; let el: HTMLElement; beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ProjectManagementComponent], schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA], imports: [HttpClientModule, RouterTestingModule, RouterModule, NgbModule, NgxPaginationModule, FormsModule, ReactiveFormsModule, BrowserModule,], providers: [{ provide: ProjectManagementServiceStub, useClass: ProjectManagementServiceStub }, { provide: ProductsService, useClass: ProductsService }, { provide: HttpClient,Http, useFactory: (backend, options) => { return new Http(backend, options); }, deps: [MockBackend, BaseRequestOptions] }, MockBackend, BaseRequestOptions,ProjectManagementService ] }) .compileComponents() })); beforeEach(async(() => { fixture = TestBed.createComponent(ProjectManagementComponent); comp = fixture.componentInstance; fixture.nativeElement.querySelectorAll('button'); })); it('should create component', () => { fixture = TestBed.createComponent(ProjectManagementComponent); comp = fixture.componentInstance; expect(comp).toBeTruthy(); }); it('should get value of toEqual', async(inject([ProjectManagementServiceStub, MockBackend], (service: ProjectManagementServiceStub, backend: MockBackend) => { backend.connections.subscribe((conn: MockConnection) => { const options: ResponseOptions = new ResponseOptions({ body: 'Project11' }); conn.mockRespond(new Response(options)); }); service.getProject("http://192.168.5.140:3002/api/project/").subscribe(res => { console.log("Subscription called") expect(res).toEqual('Project11') }) })))});app.component.service.stub.tsimport { Injectable } from '@angular/core';import { HttpClient } from '@angular/common/http';import { map } from 'rxjs/operators';import { config } from "config";import { Observable } from "rxjs";const baseUrl: string = config.url;@Injectable()export class ProjectManagementServiceStub { constructor(private http: HttpClient) { } getProject(url) :Observable<any>{ return this.http.get(url ) .pipe(map(Response => Response)) }} 解决方案 @Stevy and Shashank, Thanks for your advice. I created a separate service.spec.ts file and tested the service like this - fdescribe('ProjectManagementServiceStub', () => { let service: ProjectManagementServiceStub; let httpMock: HttpTestingController; beforeEach(()=>{ TestBed.configureTestingModule({ providers : [ ProjectManagementServiceStub] , imports: [HttpClientModule, HttpClientTestingModule,RouterTestingModule, RouterModule, NgbModule, NgxPaginationModule, FormsModule, ReactiveFormsModule, BrowserModule,] ,}) service = TestBed.get(ProjectManagementServiceStub); httpMock = TestBed.get(HttpTestingController); }) it("should be initialized ", inject([ProjectManagementServiceStub], (service1:ProjectManagementServiceStub)=>{ expect(service1).toBeTruthy(); })); it("should fetch data asynchronously", fakeAsync( inject( [ProjectManagementServiceStub, HttpTestingController], (service1: ProjectManagementServiceStub, backend : HttpTestingController)=>{const url = "http://192.168.x.xxx:3002/api/project/";const responseObject :any[]= [{ projectId: "ID123", name: 'Project1'}]let response = null;service1.getProject().subscribe( (receivedResponse : any) =>{ response = receivedResponse; console.log("Response = ", response) expect (response).toEqual(responseObject); expect(receivedResponse.length).toBe(1); }, (error: any) =>{});const requestWrapper = backend.expectOne({url :"http://192.168.x.xxx:3002/api/project/" });expect (requestWrapper.request.method). toEqual('GET');expect(requestWrapper.cancelled).toBeFalsy();requestWrapper.flush(responseObject) } ) )) afterEach(() => { httpMock.verify(); });});courtsey - https://blog.knoldus.com/unit-testing-of-angular-service-with-httpclient/https://alligator.io/angular/testing-httpclient/ 这篇关于预期的响应,状态为:null为URL无效:为null等于'Project11'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!