2017-10-08 18 views
-1

私はエラー捕捉のために自分の関数をチェックするためのテストを書いています。関数のエラーが発生すると、next()が呼び出されます。私は関数がエラーをスローし、spy.should.have.thrown(error)を使用できるように書き直したいと思います。私はエラーをスローしようとすると、しかし、私は警告を得続ける:エラーをスローするときの未処理プロミスの拒否

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: catch me

DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code

テスト:テストされているコントローラ上の

const chai = require('chai'); 
const sinon = require('sinon'); 
const sinonChai = require('sinon-chai'); 
chai.should(); 
chai.use(sinonChai); 

const mockStory = {}; 
const proxyquire = require('proxyquire'); 

//creates mock model functions to replace original model functions in controller 
const Story = proxyquire('../controller/controller', 
    { '../model/model' : mockStory} 
); 

describe('Story should be created',() => { 

    const ctx = {request: {body:'foo'}}; 

    it ('createStory should catch errors from model', async() => { 
    const foo = ctx.request.body; 
    mockStory.createStory = (foo) => { 
     throw new Error('error'); 
    }; 
    const next = sinon.spy(); 
    const res = Story.createStory(ctx, next); 
    next.should.have.been.called; 
    }); 

}); 

機能:

const mongoose = require('mongoose'); 
const Story = require('../model/model'); 
const Console = console; 

const createStory = async (ctx, next) => { 
    try { 
    const createdStory = await Story.createStory(ctx.request.body); 
    ctx.status = 200; 
    } catch (error) { 
    next(error); 
    //gives unhandled promise rejection warning... 
    throw new Error('catch me') 
    } 
}; 

module.exports = { 
    createStory, 
}; 

誰かがエラーをスローしてそのエラーをテストする方法を教えてもらえますか?

+1

'( '私をキャッチ')'新しいエラーを投げるの目的は何ですか? – guest271314

+0

エラー処理をテストできるようにエラーをスローしたい...そうでなければ、それは他の目的を果たさない... –

+1

コードは 'await Story.createStory(ctx。 request.body) 'をcatch(){}'で実行します。このコードは、明らかな理由がないので、 'catch(){}'に新しい 'Error'を作成します。 – guest271314

答えて

0

問題はコントローラが約束を返すことでした。約束どおりのライブラリを使用すると、エラーが解決されました。

//controller.js 
 
const createStory = async (ctx, next) => { 
 
    try { 
 
    const createdStory = await Story.createStory(ctx.request.body); 
 
    ctx.status = 200; 
 
    } catch (error) { 
 
    ctx.throw('Could not create story!'); 
 
    } 
 
}; 
 

 
//tests.js 
 

 
const chai = require('chai'); 
 
var chaiAsPromised = require('chai-as-promised'); 
 
chai.use(chaiAsPromised); 
 
chai.should(); 
 

 
const mockStoryModel = {}; 
 
const proxyquire = require('proxyquire'); 
 

 
const StoriesController = proxyquire('../controller/controller', 
 
    { '../model/model' : mockStoryModel} 
 
); 
 

 
describe('Story',() => { 
 

 
    const ctx = {request: {body:'foo'}}; 
 

 
    it ('createStory should catch errors from model', async() => { 
 
    const foo = ctx.request.body; 
 
    mockStory.createStory = (foo) => { 
 
     throw new Error('error'); 
 
    }; 
 
    Story.createStory().should.be.rejected; 
 
    }); 
 

 
});

関連する問題