0

I have the following function that uses bind to bind a context to the then chains. When i try and test it, it throws

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


myFunc() {
    let self = this;

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

Test

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);
    });
  });
cusejuice
  • 10,285
  • 26
  • 90
  • 145

1 Answers1

0

The hgetallAsync stub is returning a plain JS Promise rather than a Bluebird promise.

To use a Bluebird promise, you need to tell Sinon to do so using .usingPromise().

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

Documentation Link: http://sinonjs.org/releases/v4.1.2/stubs/#stubusingpromisepromiselibrary

Tristan Hessell
  • 930
  • 10
  • 21