2017-08-10 4 views
0

thenチェーンにコンテキストをバインドするためにbindを使用する次の関数があります。私は試してみて、それをテストするとき、それはユニットテストbluebird promiseバインド関数

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); 
    }); 
    }); 

答えて

0

をスローhgetallAsyncスタブは、プレーンJSの約束ではなく、Bluebird約束を返しています。

Bluebird約束を使用するには、を使用してSinonに伝える必要があります。

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

ドキュメントのリンク: http://sinonjs.org/releases/v4.1.2/stubs/#stubusingpromisepromiselibrary

関連する問題