jasmine 测试该函数是否 return 已解决的承诺

jasmine test that function does return a resolved promise

代码:

var cartModule = (function() {  
  checkOverbook: function(skip) {
    return new Promise(function(resolve, reject) {
      if (skip) {
        addItemPromiseResolver = resolve;
      } else {
        resolve({"continue_add":true})
      }
    })
  },
})();

我想测试当使用 skip = true 调用 cartModule.checkOverbook 时承诺已解决,但当使用 skip = false 调用时它未解决。这是 possible/recommended,还是我应该在消费函数的上下文中测试它?

我认为这是可能的,你可以这样做:

it('resolves if skip is false', async () => {
  const skip = false;
  await expectAsync(cartModule.checkOverbook(skip)).toBeResolvedTo({ 'continue_add': true });
});

it('changes addItemPromiseResolver to resolve if skip is true', () => {
  expect(addItemPromiseResolver).toBeUndefined();
  const skip = true;
  // just call the function
  cartModule.checkOverbook(skip);
  expect(addItemPromiseResolver).not.toBeUndefined();
});