logo
down
shadow

Angular 6 - NullInjectorError: No provider for HttpClient in unit tests


Angular 6 - NullInjectorError: No provider for HttpClient in unit tests

Content Index :

Angular 6 - NullInjectorError: No provider for HttpClient in unit tests
Tag : angular , By : Boyer C.
Date : November 28 2020, 04:01 AM

This might help you I am importing and using HttpClient in a service as follows:
import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import {HttpClientModule} from '@angular/common/http';
import { myService } from './myservice';


describe('myService', () => {

      beforeEach(() => TestBed.configureTestingModule({
        imports: [HttpClientTestingModule], 
        providers: [myService]
      }));

       it('should be created', () => {
        const service: myService = TestBed.get(myService);
        expect(service).toBeTruthy();
       });

       it('should have getData function', () => {
        const service: myService = TestBed.get(myService);
        expect(service.getData).toBeTruthy();
       });

    });

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

NullInjectorError: No provider for HttpClient! Angular 5


Tag : asp.net-mvc , By : dexteryy
Date : March 29 2020, 07:55 AM
will be helpful for those in need HttpClient needs for the module HttpClientModule instead of HttpModule to be imported and added in the imports of the module.
For more see Documentation
import { HttpClientModule } from '@angular/common/http';

@NgModule({
    declarations: [
        ...
    ],
    imports: [
        ...
        HttpClientModule,
        ...
    ]
})
export class AppModuleShared { }

Angular 6 service failing to pass unit tests with (NullInjectorError: No provider for HttpClient!)


Tag : javascript , By : judith
Date : March 29 2020, 07:55 AM
To fix the issue you can do I believed that you issue relies upon the TestBed definition you'are doing this twice, potentially this is the issue:
 //first testbed definition, move this into the beforeEach
TestBed.configureTestingModule({
  imports: [HttpClientTestingModule, HttpClientModule],
  providers: [UserService]
});

describe('UserService', () => {
  beforeEach(() => TestBed.configureTestingModule({})); <- second one, cleanning the module definition before each test

   it('should be created', () => {
    const service: UserService = TestBed.get(UserService);
    expect(service).toBeTruthy();
   });

   it('should have add function', () => {
    const service: UserService = TestBed.get(UserService);
    expect(service.addComments).toBeTruthy();
   });

});
describe('UserService', () => {

  beforeEach(() => TestBed.configureTestingModule({
    imports: [HttpClientTestingModule], // <- notice that I remove the HttpClientModule
    providers: [UserService]
  }));

   it('should be created', () => {
    const service: UserService = TestBed.get(UserService);
    expect(service).toBeTruthy();
   });

   it('should have add function', () => {
    const service: UserService = TestBed.get(UserService);
    expect(service.addComments).toBeTruthy();
   });

});

Angular : NullInjectorError: No provider for HttpClient


Tag : angular , By : Richard
Date : March 29 2020, 07:55 AM
To fix the issue you can do I have implemented a simple angular application that has a component that calls a service which in turn calls an api. I have registered the service in the provider of the app module but getting the following error . , Add
import { HttpClientModule } from '@angular/common/http'; 

@NgModule({
  imports:      [ HttpClientModule ]
})

Angular 5 service failing to pass unit tests with (NullInjectorError: No provider for HttpClient!)


Tag : angular , By : arbeitandy
Date : March 29 2020, 07:55 AM
wish help you to fix your issue The reason for "NullInjectorError: No provider for HttpClient!" are unresolved dependencies. In this case the lack of a HttpClientModule.
In your .service.spec.ts file add
  imports: [
        HttpClientTestingModule,
    ],

NullInjectorError: No provider for HttpClient! Unit Testing a service


Tag : angular , By : hsdfhksh
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I have been trying to unit test a basic service today and I keep getting this error: Error: StaticInjectorError[HttpClient]: StaticInjectorError[HttpClient]: NullInjectorError: No provider for HttpClient! "Error: StaticInjectorError". , You need to import
import { HttpClientModule } from '@angular/common/http';
@NgModule({
  imports: [BrowserModule, FormsModule, MatFormFieldModule, MatInputModule, BrowserAnimationsModule, HttpClientModule],
  providers: [VisitsIngestedService],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
Related Posts Related QUESTIONS :
  • Angular Get Parameter from Url with Special Character
  • Waiting for Subcription to finish in Angular
  • Jest Unit Tests do not work started with WebStorm Jest Testrunner
  • Calling ts. function from HTML to change its parameters
  • Angular Multiple Templates - With Multiple Components
  • Can't display static image in the same folder as the component in Angular
  • Angular 6: Why aren't the results of Next visible immidiately?
  • Angular Material: How to set Sidenav/Drawer to be open by default?
  • Angular 4 animation on children: style jumps back
  • Angular 4+ ngClass Mix Expressions
  • class hierarchy in TypeScript
  • How to generate components in a specific folder with angular-cli?
  • How to add user to database and authorize user as well
  • Angular - how do I get data to load with ngOnChanges
  • How to exclude livereload dependency from NestJS/Angular Universal?
  • Static member is not accessible error on navbar component
  • property does not exists on type Object from subscribe
  • Replace nested promise.all with observable stream to aggregate over an array
  • How do you make a "please wait" spinner for Angular http requests (or anything really)?
  • ngClass - apply multiple classes from variables?
  • In Angular 7, how do I extract the result from an Observable?
  • CKEditor 5 - Angular 7 : object ngModel binding with sub childs problem
  • How to not re-initialise child component when parent property changes?
  • How to pass data to component to another component
  • How to create a selector to affine view (like a filter) through rest api
  • Angular array says its an object?
  • Enable full template type-checking in Angular application
  • How to isolate private static functions in Angular / Typescript?
  • Angular error when changing font color from API value
  • Angular | How do i show an object inside the form of another component
  • Angular - generate array of objects and display the data
  • How to load component into another
  • Set Node options when running with Angular-CLI
  • View does not update when calling function from other component Angular + NativeSript(6)
  • The fields in my form are not binding to the model attributes in Angular
  • Can someone help me out here, can't get the routing to work properly, keep getting 404
  • Providing/Injecting third party service to component while angular component unit testing
  • Using Reactive Forms to send value from field wrapped in compulsory object
  • how to get http response from typescript post
  • how to get public IP in Angular Project
  • Access Nested Models from function Angular
  • Angular 7 - Set the data source for the Select/dropdown box
  • Angular Material Dialogue issue - Error: The selector "dialog-result-example-dialog" did not match any element
  • BodyParser doesn't parse - but everything looks right to me. How to resolve `Cannot read property of undefined`
  • Deep Linking with IOS and Android Nativescript
  • Angular 8 + IE 11: Some polyfills are missed
  • How access form attributes from component?
  • Pass all FromGroup control values to another FormGroup
  • In Angular 7, how do I access the component that emitted an event?
  • Not able to call a method when a feature/marker is selected in OpenLayers
  • How to Implement Nested NG If else conditions in Angular 8
  • Want only single true or false value from observers
  • How to add react custom element in angular 6
  • StaticInjectorError(AppModule)[MainComponent -> MatDialog]
  • Loop through data set showing undefined
  • Why does the Angular Material table mat-paginator pagesize not work?
  • Can Web API get input from angular client for confirmation and continue execution?
  • Will the angular build include the spec file size?
  • Meta tags angular 7
  • Can't find file on stackblitz Angular project
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com