可观察的角度测试开关语句

问题描述 投票:0回答:1

我是Angular的新手,我想弄清楚如何在茉莉花测试中的可观察对象内部测试switch语句。有什么想法吗?

任何帮助都会很棒。

private getConditionTypes(): void {

this.listDataService.getConditionTypes().subscribe(data => {

  data.forEach(ct => {
    switch (ct.code) {
      case 'LT':
        ct.symbol = '<';
        break;
      case 'GT':
        ct.symbol = '>';
        break;
      case 'ET':
        ct.symbol = '=';
        break;
      case 'LTE':
        ct.symbol = '<=';
        break;
      case 'GTE':
        ct.symbol = '>=';
        break;
    }
  });
  this.listDataStore.updateConditionTypes(data);
});

}

angular typescript jasmine switch-statement observable
1个回答
0
投票

只需遵循文档:

function asyncData<T>(data: T) {
  return defer(() => Promise.resolve(data));
}

describe('Example test', () => {
  let component: MyComponent;
  let fixture: ComponentFixture<MyComponent>;

  let getConditionTypesSpy: any;

  beforeEach(() => {

    const fakeListDataService = jasmine.createSpyObj('ListDataService', ['getConditionTypes']);

    getConditionTypesSpy = fakeListDataService.getConditionTypes.and.returnValue('>');

    TestBed.configureTestingModule({
      declarations: [ MyComponent ],
      providers:    [
        { provide: ListDataService, useValue: fakeListDataService }
      ]
    });

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

  it('should test greater then in switch', fakeAsync(() => {

    // call getConditionTypes (it'll call the observable)
    component.getConditionTypes();

    fixture.detectChanges();

    tick();

    // check whatever you want


  }));
});

© www.soinside.com 2019 - 2024. All rights reserved.