dimanche 27 janvier 2019

Angular unit testing multiple getters on a singular service

So I'm trying to test the calls that component x make to it's injected service, the service it self has a couple of methods, and rxjs BehaviourSubject getters/setters.

Currently testing one getter like so.

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);
  }))

And this works fine passes and covers code coverage, but how do I go about testing the other getter calls on the service and methods, as I can't have more than one mockValue assigned to a service.

Normally I would just create spyObj for the methods, but I can't seem to assign more than useValue to the service or store all the getters/methods in one variable.

Aucun commentaire:

Enregistrer un commentaire