如何模拟角度模型

时间:2019-03-01 13:04:43

标签: angular unit-testing karma-jasmine

我有一个接受模型作为输入的函数

  selectFault(fault: Fault) {
    this.faultsService.setSelectedFault(fault)
      this.router.navigate(['fault-detail']);

  }

我已经为此进行了测试,但是它抱怨有故障根源

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;

}

0 个答案:

没有答案