I am running unit test on Angular App, But the component is not getting intialized. It needs three paramaters, I will mock the service but what about the first two parameters?
import { HttpClient } from '@angular/common/http';
import { NgxSpinnerService } from 'ngx-spinner';
constructor(private http: HttpClient,
private spinner: NgxSpinnerService,
private someService: SomeService) {
}
Expected: Component should create
Actual: Component is not created
import HttpClientTestingModule for httpClient and add NgxSpinnerModule in your imports too
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { NgxSpinnerModule } from 'ngx-spinner';
...
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule, // for httpClient
NgxSpinnerModule
],
declarations: [ YourComponent],
providers: [service],
schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents();
fixture = TestBed.createComponent(YourComponent);
component = fixture.componentInstance;
}));
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { NgxSpinnerModule } from 'ngx-spinner';
...
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule, // for httpClient
NgxSpinnerModule
],
declarations: [ YourComponent],
providers: [service],
schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents();
}));
beforeEach(async() => {
fixture = TestBed.createComponent(AnalyticsComponent);
component = fixture.componentInstance;
// @Inputs
component.metric = 'sales';
component.ngOnInit(); // <===== importand
component.ngAfterViewInit(); // <===== importand
await fixture.whenStable(); // <===== importand
fixture.detectChanges(); // <===== importand
});
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.