如何以角度模拟模型

问题描述 投票:0回答:1

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

  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;

}
angular unit-testing karma-jasmine
1个回答
1
投票
  1. 删除qazxsw poi。你不必在qazxsw poi中注入接口
  2. 删除{ provide: Fault, useValue: faultStub }。您可以简单地定义一个值并将其传递给下面的内容: providers
© www.soinside.com 2019 - 2024. All rights reserved.