angular2 testing using jasmine for subscribe method

前端 未结 4 1502
慢半拍i
慢半拍i 2020-12-29 05:21

I have a spec code to test like this

 it(\'login test\', () => {

      const fixture = TestBed.createComponent(component);
      fixture.detectChanges();         


        
4条回答
  •  感动是毒
    2020-12-29 05:57

    Change your spy for the 'login' method on your authService to return an observable instead of a value. You'll need to import:

    import 'rxjs/add/observable/from';
    import {Observable} from 'rxjs/Observable';
    

    Setup your spy:

    const loginResult = '';
    const spy = spyOn(authService, 'login').and.callFake(() => {
        return Observable.from([loginResult]);
    })
    

    Call login:

    fixture.componentInstance.login();
    

    Assert:

    expect(spy).toHaveBeenCalled();
    

提交回复
热议问题