代码之家  ›  专栏  ›  技术社区  ›  Munerz

角度单位测试单一服务上的多个吸气剂

  •  0
  • Munerz  · 技术社区  · 7 年前

    所以我试着测试一下 部件x

    目前正在测试一个这样的getter。

    let component: EditFeatureComponent;
      let fixture: ComponentFixture<EditFeatureComponent>;
      let vectorLayerSubject = new BehaviorSubject<any>({});
      let mockAnnoService = { getLayerToEdit: vectorLayerSubject.asObservable() };
      let mockFeatureService = jasmine.createSpyObj(['getFeatureToEdit', 'setFeatureToEdit']);
    
      beforeEach(async(() => {
        TestBed.configureTestingModule({
          declarations: [EditFeatureComponent, CoreMapComponent, MapsComponent],
          imports: [FormsModule, HttpClientModule],
          providers: [
            { provide: AnnotationService, useValue: mockAnnoService},
            { provide: FeatureStyleService, useValue: mockFeatureService}
          ],
          schemas: [CUSTOM_ELEMENTS_SCHEMA]
        })
          .compileComponents();
      }));
    
      beforeEach(() => {
        // Fields required for the component
        layer = {
          layerName: 'testLayer',
          ol_uid: 1
        }
        // Create the testbed
        fixture = TestBed.createComponent(EditFeatureComponent);
        component = fixture.componentInstance;
      });
    
      it('Should return a layer object and have a ol_uid of 1', fakeAsync(()=>{
        vectorLayerSubject.next(layer);
        component.ngOnInit();
        expect(component.layer).toBe(layer);
      }))
    

    通常,我只为方法创建spyObj,但似乎不能为服务分配超过useValue的值,也不能将所有getter/methods存储在一个变量中。

    我还应该注意到,服务是私有的,在组件上是只读的。

    0 回复  |  直到 7 年前