注入 MdDialogRef 的测试组件正在调用服务
Testing component with MdDialogRef injected is calling service
我在测试具有 MdDialogRef 和注入服务的组件时遇到问题。我想测试这个组件是否正在调用注入的服务。问题是我无法用通常的方式检索服务
fixture = TestBed.createComponent(...);
component = fixture.componentInstance;
service = fixture.debugElement.injector.get(Service);
因为必须像这样检索注入了 MDDialogRef 的组件:
dialog = TestBed.get(MdDialog);
dialogRef = dialog.open(CompanyLogoComponent);
component = dialogRef.componentInstance;
这是 MdDialogRef 的解决方法,它表示 'no provider available for MdDialogRef',并且在提供需要许多参数时。 (也许有更好的方法如何做到这一点然后使用夹具?)
因此,没有可用于检索服务的固定装置...'debugElement.injector...'
注入服务时,我有另一个范围,因为间谍没有反应:
it('method should call service', inject ([Service], (service: Service) => {
expect(service).toBeTruthy();
spyOn(service, 'method').and.callThrough();
component.methodCallingService();
expect(service.method).toHaveBeenCalled();
}));
知道如何在此处将范围绑定到组件或通过组件检索服务吗(dialogRef.componentInstance)?
我该如何解决这个问题:
在 TestComponent 中,注入 MdDialog 并设置 dialogRef:
public dialogRef: MdDialogRef<TestComponent>;
constructor(private dialog: MdDialog, private service: TestService){}
里面TestComponent.spec,可以像往常一样通过fixture获取TestService
describe('TestComponent', () => {
let component: TestComponent;
let testService;
let fixture;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
MaterialModule,
MdDialogModule],
declarations: [TestComponent],
providers: [TestService]
})
.overrideModule(BrowserDynamicTestingModule, {
set: {
entryComponents: [TestComponent]
}
})
.overrideComponent(TestComponent, {
set: {
providers: [
{provide: TestService, useClass: MockTestService},
]
}
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
companyService = fixture.debugElement.injector.get(TestComponent);
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
要设置 entryComponents,您可以覆盖 BrowserDynamicTestingModule。
我在测试具有 MdDialogRef 和注入服务的组件时遇到问题。我想测试这个组件是否正在调用注入的服务。问题是我无法用通常的方式检索服务
fixture = TestBed.createComponent(...);
component = fixture.componentInstance;
service = fixture.debugElement.injector.get(Service);
因为必须像这样检索注入了 MDDialogRef 的组件:
dialog = TestBed.get(MdDialog);
dialogRef = dialog.open(CompanyLogoComponent);
component = dialogRef.componentInstance;
这是 MdDialogRef 的解决方法,它表示 'no provider available for MdDialogRef',并且在提供需要许多参数时。 (也许有更好的方法如何做到这一点然后使用夹具?)
因此,没有可用于检索服务的固定装置...'debugElement.injector...'
注入服务时,我有另一个范围,因为间谍没有反应:
it('method should call service', inject ([Service], (service: Service) => {
expect(service).toBeTruthy();
spyOn(service, 'method').and.callThrough();
component.methodCallingService();
expect(service.method).toHaveBeenCalled();
}));
知道如何在此处将范围绑定到组件或通过组件检索服务吗(dialogRef.componentInstance)?
我该如何解决这个问题:
在 TestComponent 中,注入 MdDialog 并设置 dialogRef:
public dialogRef: MdDialogRef<TestComponent>;
constructor(private dialog: MdDialog, private service: TestService){}
里面TestComponent.spec,可以像往常一样通过fixture获取TestService
describe('TestComponent', () => {
let component: TestComponent;
let testService;
let fixture;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
MaterialModule,
MdDialogModule],
declarations: [TestComponent],
providers: [TestService]
})
.overrideModule(BrowserDynamicTestingModule, {
set: {
entryComponents: [TestComponent]
}
})
.overrideComponent(TestComponent, {
set: {
providers: [
{provide: TestService, useClass: MockTestService},
]
}
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
companyService = fixture.debugElement.injector.get(TestComponent);
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
要设置 entryComponents,您可以覆盖 BrowserDynamicTestingModule。