可观察订阅中的单元测试功能

时间:2018-07-26 07:35:22

标签: unit-testing ionic3 observable

我要测试以下代码

@Injectable()
export class MyService() {
  constrcutor(private ble: BLE) {}

  method(id: string) {
    this.ble.connect(device.id).subscribe(() => {
        this.ble.requestMtu(...);
    });
  }
}

我要检查的是this.ble.requestMtu成功完成后,调用了this.ble.connect

我尝试了一些方法,但是都没有。

这是我到目前为止所拥有的

describe('xxx', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [DevicesServiceBLE, BLE, Platform]
    });
  });

  it('yyy', inject([MyService], (service, done) => {
    let bleConnect = spyOn(BLE.prototype, 'connect').and.callFake(() => {
      let obs = new EmptyObservable();
      obs.subscribe(() => {
        console.log('I am called!!!');
        expect(bleRequestMtu).not.toHaveBeenCalled();
        done();
      });
      return obs;
    });
    let bleRequestMtu = spyOn(BLE.prototype, 'requestMtu').and.returnValue(
      new Promise(() => {})
    );

    service.connect({ id: 'ID' });
    expect(bleConnect).toHaveBeenCalledWith('ID');
  }));
});

但是似乎从未达到expect(bleRequestMtu).not.toHaveBeenCalled();

0 个答案:

没有答案