简体   繁体   English

Jasmine单元测试可观察订阅不会触发

[英]Jasmine unit testing observable subscribe does not trigger

I'm using Angular 5 with Jasmine and Karma. 我正在使用Angular 5与Jasmine和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. 这导致我的单元测试失败,因为我正在使用jasmine的done函数。 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). 我已经将超时间隔设置为20秒,看它是否只需要一段时间(它不应该)。

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 这是与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();
  });
});

If service.ItemsToFilterBy$ is Subject<any>() , not BehaviorSubject or ReplaySubject , subscribe to service.ItemsToFilterBy$ in the beginning of your spec. 如果service.ItemsToFilterBy$Subject<any>() ,而不是BehaviorSubjectReplaySubject ,请在规范的开头订阅service.ItemsToFilterBy$ Read here . 在这里阅读

Use the callThrough() method by jasmine when you spy on a method. 使用callThrough()的方法, jasmine ,当你在一个方法窥探。 spyOn(service.ItemsToFilterBy$, 'subscribe')

ie

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM