mardi 18 décembre 2018

Testing service call from my component unit test

How would I simply test that my service is called?

myComp.ts

constructor(private myService: MyService) {}

myFunction() {
   this.myService.someFunction();
}

myTest.ts

beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [ReactiveFormsModule, FormsModule, HttpClientModule],
      declarations: [MyComponent]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should delete customer feed', () => {
    const mySpy = spyOn(MyService, 'someFunction');

    component.myFunction();

    expect(mySpy).toHaveBeenCalledTimes(1);
  });

Currently the line const mySpy = spyOn(MyService, 'someFunction'); shows red squigglies on 'someFunction' stating:

Argument of type 'someFunction' is not assignable to parameter of type prototype | service | url

Aucun commentaire:

Enregistrer un commentaire