简体   繁体   中英

Angular 9 Unit Test Karma

I cannot remove this karma error which claims about the http module, I have tried various ways and I cannot solve it. My application is in angular 9

报告

Project structure:

app Components Login.component.ts Login.component.spec.ts Services Login.service.ts Login.service.spec.ts

The services services are added in the file app.module.ts

Grettings!

This is my Login.service.spec.ts

Try adding HttpClient in the providers of your TestBed configuration.. All your test cases mutually exclusive from the application so you have nothing to do with app.module.ts.

Try this.

TestBed.configureTestingModule({ providers: [ HttpClient ] });

OR else try this https://angular.io/guide/http#testing-http-requests

Angular provides a test version of the HttpClientModule that you can use in your tests. It makes sure the Http requests does not actually run when your tests run. You add it in the imports array of your Testbed like:

imports: [HttpClientTestingModule]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM