2017-10-08 10 views
-1

J'ai écrit un test pour vérifier ma fonction de détection d'erreur. Lorsque les erreurs de fonction, next() sera appelée. Je voudrais le réécrire donc la fonction jette une erreur et je peux utiliser spy.should.have.thrown(error). Cependant, lorsque je tente de lancer une erreur, je continue à recevoir des avertissements:Non promis Promesse Rejet en cas d'erreur de lancement

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

Le test:

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

}); 

La fonction du contrôleur testé:

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

Quelqu'un peut-il me dire comment je peux lancer une erreur et tester cette erreur?

+1

Quel est le but de 'lancer une nouvelle erreur ('catch me')'? – guest271314

+0

Je veux qu'il jette une erreur afin que je puisse tester la gestion des erreurs ... sinon, maintenant, il ne sert à rien d'autre ... –

+1

Le code gère l'erreur de 'attend Story.createStory (ctx. request.body) 'at' catch() {} '. Le code crée également un nouveau 'Error' à' catch() {} 'sans raison apparente. – guest271314

Répondre

0

Le problème était que le contrôleur retournait une promesse. L'utilisation de la bibliothèque chai-as-promise a résolu l'erreur.

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

 
});