可观察到的Angular Testing开关语句

时间:2020-04-27 23:46:46

标签: angular typescript jasmine switch-statement observable

我是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);
});

}

1 个答案:

答案 0 :(得分: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


  }));
});

相关问题