Jasmine unit testing observable subscribe does not trigger

喜夏-厌秋 提交于 2019-12-07 15:06:50

问题


I'm using Angular 5 with Jasmine and Karma. I'm trying to test whether a certain function works but my subscribe doesn't trigger during unit testing. this causes my unit test to fail as I'm using the done function of jasmine. I want to make this unit test succeed.

I've set the timeout interval to 20 seconds to see if it just took a while (which it shouldn't).

I've tried to use async and fakeasync as well, but it doesn't trigger. Is it possible for me to make the subscription to trigger?

this is the code ive got:

describe('FilterService', () => {
  let service: FilterService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [FilterService]
    });
    service = TestBed.get(FilterService);
    jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000;
  });

  it("SetItemsToFilterByAndApplyFilters should set ItemsToFilterBy and set the InFilter property", (done: DoneFn) => {
    //arrange
    let item1: any = new Organisation();
    item1.Id = 1;
    item1.InFilter = true;
    let item2: any = new Organisation();
    item1.Id = 2;
    item1.InFilter = false;

    let itemNew1: any = new Organisation();
    itemNew1.Id = 1;
    let itemNew2: any = new Organisation();
    itemNew2.Id = 2;

    service.SetItemsToFilterBy([item1, item2]);
    let spy = spyOn(service.ItemsToFilterBy$, 'subscribe');

    //act
    service.SetItemsToFilterByAndApplyFilters([itemNew1, itemNew2]);
    //assert
    service.ItemsToFilterBy$.subscribe(result => {
      let result1 = _.find(result, item => {
        return item.Id == itemNew1.Id;
      });

      let result2 = _.find(result, item => {
        return item.Id == itemNew2.Id;
      });

      expect(result1.InFilter).toBeTruthy();
      expect(result2.InFilter).toBeFalsy();
      done();
    });
  });
});

this is the code that worked with thanks to Basavaraj Bhusani

      it("SetItemsToFilterByAndApplyFilters should set ItemsToFilterBy and set the InFilter property", (done: DoneFn) => {

    //arrange
    let spy = spyOn(service.ItemsToFilterBy$, 'subscribe').and.callThrough();

    let item1: any = new Organisation();
    item1.Id = 1;
    item1.InFilter = true;
    let item2: any = new Organisation();
    item2.Id = 2;
    item2.InFilter = false;

    let itemNew1: any = new Organisation();
    itemNew1.Id = 1;
    let itemNew2: any = new Organisation();
    itemNew2.Id = 2;

    service.SetItemsToFilterBy([item1, item2]);

    //act
    service.SetItemsToFilterByAndApplyFilters([itemNew1, itemNew2]);

    //assert
    service.ItemsToFilterBy$.subscribe(result => {
      let result1 = _.find(result, item => {
        return item.Id == itemNew1.Id;
      });

      let result2 = _.find(result, item => {
        return item.Id == itemNew2.Id;
      });

      expect(result1.InFilter).toBeTruthy();
      expect(result2.InFilter).toBeFalsy();
      done();
    });
    expect(spy).toHaveBeenCalled();
  });
});

回答1:


If service.ItemsToFilterBy$ is Subject<any>(), not BehaviorSubject or ReplaySubject, subscribe to service.ItemsToFilterBy$ in the beginning of your spec. Read here.

Use the callThrough() method by jasmine when you spy on a method. spyOn(service.ItemsToFilterBy$, 'subscribe')

i.e.

const spy = spyOn(service.ItemsToFilterBy$, 'subscribe').and.callThrough();

Read about callThrough here https://jasmine.github.io/2.0/introduction.html



来源:https://stackoverflow.com/questions/49426466/jasmine-unit-testing-observable-subscribe-does-not-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!