Jasmine 有没有办法监视承诺构造函数?

Jasmine 有没有办法监视承诺构造函数?,jasmine,jestjs,Jasmine,Jestjs,我有这个功能 public pick(config?: FilePickerConfig): Promise<FilePickerResult> { return new Promise<FilePickerResult>(resolve => { this.pickWithCallbacks(resolve, resolve, config); }); } 所以最后,我只是离开了承诺做他的事情,我抓住了决心 test('

我有这个功能

public pick(config?: FilePickerConfig): Promise<FilePickerResult> {
    return new Promise<FilePickerResult>(resolve => {
      this.pickWithCallbacks(resolve, resolve, config);
    });
  }


所以最后,我只是离开了承诺做他的事情,我抓住了决心

    test('on success should call pickWithCallbacks with the resolve function of a promise', (done) => {
      const cordovaExecSpy = spyOn(sut, 'pickWithCallbacks');
      const dummyReturn = {};
      sut.pick().then(obtained => {
        expect(obtained).toBe(dummyReturn);
        done();
      });

      const capturedOnSucess = cordovaExecSpy.calls.mostRecent().args[0];
      capturedOnSucess(dummyReturn);
    });

    test('on Error should call pickWithCallbacks with the resolve function of a promise', (done) => {
      const cordovaExecSpy = spyOn(sut, 'pickWithCallbacks');
      const dummyReturn = {};
      sut.pick().then(obtained => {
        expect(obtained).toBe(dummyReturn);
        done();
      });

      const capturedOnError = cordovaExecSpy.calls.mostRecent().args[1];
      capturedOnError(dummyReturn);
    });


你能添加ypu尝试过的代码吗?@JonathanLarouche我会看看我的ctrl-z历史记录中是否有它。我正在尝试很多东西。你能不能用jest.fn监视函数pickWithCallbacks?例如:模拟方法:obj.pickWithCallbacks=jest.fn;然后调用pick方法obj.pickconfig;并在验证结果expectobj.pickWithCallbacks.To后,使用valueForResolve、valueForResolve、valueForConfig调用@JonathanLarouche我试过类似的东西。我已经更新了我的问题很高兴你让它工作了,我更习惯于用jest.fn而不是spyOn编写代码。谢谢分享你的答案
    test('on success should call pickWithCallbacks with the resolve function of a promise', (done) => {
      const cordovaExecSpy = spyOn(sut, 'pickWithCallbacks');
      const dummyReturn = {};
      sut.pick().then(obtained => {
        expect(obtained).toBe(dummyReturn);
        done();
      });

      const capturedOnSucess = cordovaExecSpy.calls.mostRecent().args[0];
      capturedOnSucess(dummyReturn);
    });

    test('on Error should call pickWithCallbacks with the resolve function of a promise', (done) => {
      const cordovaExecSpy = spyOn(sut, 'pickWithCallbacks');
      const dummyReturn = {};
      sut.pick().then(obtained => {
        expect(obtained).toBe(dummyReturn);
        done();
      });

      const capturedOnError = cordovaExecSpy.calls.mostRecent().args[1];
      capturedOnError(dummyReturn);
    });