0

我有以下用于bind将上下文绑定到then链的函数。当我尝试测试它时,它会抛出

  TypeError: redisClient.hgetallAsync(...).bind is not a function


myFunc() {
    let self = this;

    return redisClient.hgetallAsync('abcde')
      .bind({ api: self })
      .then(doStuff)
      .catch(err => {
        // error
      });
  }

测试

let redisClient = { hgetallAsync: sinon.stub() };

describe('myFunc', () => {
    beforeEach(() => {
      redisCLient.hgetallAsync.resolves('content!');
    });

    it('should do stuff', () => {
      return myFunc()
        .should.eventually.be.rejectedWith('Internal Server Error')
        .and.be.an.instanceOf(Error)
        .and.have.property('statusCode', 500);
    });
  });
4

1 回答 1

0

存根返回一个普通的hgetallAsyncJS Promise 而不是一个 Promise Bluebird

要使用BluebirdPromise,您需要Sinon使用.usingPromise().

let redisClient = { hgetallAsync: sinon.stub().usingPromise(bluebird.Promise) };

文档链接: http ://sinonjs.org/releases/v4.1.2/stubs/#stubusingpromisepromiselibrary

于 2017-11-10T01:22:24.910 回答