【问题标题】:Test switch case in JasmineJasmine 中的测试开关盒
【发布时间】:2020-12-03 14:26:22
【问题描述】:

我正在学习用 jasmine 编写测试用例,我有一个包含如下 switch 语句的函数

public onMReceived(event) {
    switch (event && event['event']) {
      case 'onOplected':
        this.Next();
        break;

      case 'temp':
        break;
    }
  }

我已经编写了正确的测试用例,当案例是 onOplected 但覆盖另一个案例时,案例是'temp'

 it('should call temp on onOplected value in switch', () => {
    //const toggleSpy = spyOn<any>(component, 'Next');
    component.ngOnInit();
    fixture.detectChanges();
    component.onMessageReceived({ event: 'temp', data: {} });
    // expect(toggleSpy).toHaveBeenCalledWith(undefined);
  });

我应该如何写期望,如果有人可以帮助,请指导。

【问题讨论】:

    标签: unit-testing jasmine tdd karma-jasmine


    【解决方案1】:

    您可以测试下一个没有被调用或类似的东西。

     it('should do nothing if the event is temp and not call next', () => {
        const toggleSpy = spyOn<any>(component, 'Next');
        component.ngOnInit();
        fixture.detectChanges();
        component.onMessageReceived({ event: 'temp', data: {} });
        // also change your toHaveBeenCalledWith(undefined) to .toHaveBeenCalled() in your previous test
        expect(toggleSpy).not.toHaveBeenCalled(); // put .not.toHaveBeenCalled();
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-22
      • 1970-01-01
      • 2016-01-26
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 2017-03-24
      相关资源
      最近更新 更多