我有一个接受模型作为输入的函数
selectFault(fault: Fault) {
this.faultsService.setSelectedFault(fault)
this.router.navigate(['fault-detail']);
}
我已经为它生成了一个测试,但它正在抱怨faultstub
error TS2693: 'Fault' only refers to a type, but is being used as a value here.
它抱怨的是
{ provide: Fault, useValue: faultStub }
整个测试完全如下
那么如何进行新的故障传递给我的功能?
let component: Tab1Page;
let fixture: ComponentFixture<Tab1Page>;
beforeEach(() => {
const routerStub = { navigate: () => ({}) };
const faultsServiceStub = {
getFaults: () => ({ subscribe: () => ({}) }),
invalidateCache: () => ({}),
setSelectedFault: () => ({})
};
const faultStub = {};
TestBed.configureTestingModule({
schemas: [NO_ERRORS_SCHEMA],
declarations: [Tab1Page],
providers: [
{ provide: Router, useValue: routerStub },
{ provide: FaultsService, useValue: faultsServiceStub },
{ provide: Fault, useValue: faultStub }
]
});
fixture = TestBed.createComponent(Tab1Page);
component = fixture.componentInstance;
});
it('can load instance', () => {
expect(component).toBeTruthy();
});
describe('selectFault', () => {
it('makes expected calls', () => {
const routerStub: Router = fixture.debugElement.injector.get(Router);
const faultsServiceStub: FaultsService = fixture.debugElement.injector.get(
FaultsService
);
const faultStub: Fault = fixture.debugElement.injector.get(Fault);
spyOn(routerStub, 'navigate');
spyOn(faultsServiceStub, 'setSelectedFault');
component.selectFault(faultStub);
expect(routerStub.navigate).toHaveBeenCalled();
expect(faultsServiceStub.setSelectedFault).toHaveBeenCalled();
});
});
这是我的错
export interface Fault {
description: string;
title: string;
assignee_id: Number;
id: string;
}
{ provide: Fault, useValue: faultStub }
。您可以简单地定义一个值并将其传递给下面的内容:
providers