我家的规格是:
fdescribe('HomeComponent', () => {
let component: HomeComponent;
let fixture: ComponentFixture<HomeComponent>;
let service: HomeService;
beforeEach(async(() => {
TestBed.configureTestingModule({
schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA],
providers: [
{provide: HomeService, useClass: HomeServiceStub}
],
declarations: [HomeComponent]
}).compileComponents();
}));
beforeEach(async(() => {
fixture = TestBed.createComponent(HomeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
service = TestBed.get(HomeService);
}));
fit('should create', () => {
expect(component).toBeTruthy();
});
it('should call get user details from homeService onInit', () => {
spyOn(service, 'getUserDetails').and.callThrough();
component.ngOnInit();
expect(service.getUserDetails).toHaveBeenCalled();
});
it('should dataplan submission page on dataplan submission click ', () => {
spyOn(service, 'onDataPlanCall').and.callThrough();
component.dataPlanSubmission();
expect(service.onDataPlanCall).toHaveBeenCalled();
});
});
我的组件是:
import {
Component,
OnInit
} from '@angular/core';
import {HomeService} from '../../shared/home/home.service';
import {Router} from '@angular/router';
import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
import {UploadComponent} from '../upload/upload.component'
@Component({
selector: 'home', // <home></home>
providers: [
],
styleUrls: [ './home.component.scss' ],
templateUrl: './home.component.html'
})
export class HomeComponent implements OnInit {
constructor(private homeService: HomeService, private _ngbModal: NgbModal) {
}
public ngOnInit() {
this.homeService.getUserDetails();
}
toggleUploadModal() {
this._ngbModal.open(UploadComponent, {size: 'lg'})
}
dataPlanSubmission() {
this.homeService.onDataPlanCall();
}
}
测试时得到的错误是:
PhantomJS 2.1.1 (Windows 7 0.0.0) HomeComponent should create FAILED
Failed: StaticInjectorError(DynamicTestModule)[HomeComponent -> NgbModal]:
StaticInjectorError(Platform: core)[HomeComponent -> NgbModal]:
NullInjectorError: No provider for NgbModal!
答案 0 :(得分:2)
您需要将与NgbModal
对应的模块导入您的试验台。
TestBed.configureTestingModule({
schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA],
providers: [
{provide: HomeService, useClass: HomeServiceStub}
],
declarations: [HomeComponent],
imports: [NgbModalModule] // I don't know the name
}).compileComponents();