如何测试主题是否已订阅茉莉花

时间:2019-07-07 11:00:23

标签: angular jasmine karma-jasmine

我在要进行单元测试的组件中有一个简单的实现:

ngOnInit()
{
  this.subscribeToResponse();
}     

subscribeToResponse()
{

this.service.responseSubject.pipe(takeUntil(this.subscribe$)).subscribe(response => {
});
}

规范文件:

describe('myComponent', () => {
  let component: myComponent;
  let fixture: ComponentFixture<myComponent>;



  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ myComponent ],
      providers: [service]
    })
    .compileComponents();
  }));

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

  fit('should create AddNewAppointmentComponent', () => {
    expect(component).toBeTruthy();
  });

  fit('Should subscribe to response from service', ()=>{

    // Arrange
    let service = TestBed.get(service);
    let spy = spyOn(service, 'responseSubject').and.callFake.({ subscribe: () => {} });

    //Act 
    component.subscribeToResponse();
    fixture.detectChanges();
    //Assert
    expect(spy).toHaveBeenCalled();

  })
});

但是我得到:

  

预计将被召唤的间谍。

调用函数后,必须执行哪些操作来检查组件中是否订阅了某个主题?

更新

下面是我订阅主题的函数的主体(示例),订阅后,我会根据主题的响应执行一些操作:

this.service.responseSubject.pipe(takeUntil(this.subscribe$)).subscribe(response => {

      if(response[results])
      {
          this.moveToStep2();
      }
      else()
      {
          this.moveToStep1();
      }
    });
    }

我想测试是否返回了某些响应,然后调用了一个函数,但我不知道该如何模拟这种行为。

0 个答案:

没有答案