Skip to content

Instantly share code, notes, and snippets.

@isabellachen
Last active October 10, 2017 13:41
Show Gist options
  • Save isabellachen/fcffbf2b3567a0299a3a06bd2f8582d4 to your computer and use it in GitHub Desktop.
Save isabellachen/fcffbf2b3567a0299a3a06bd2f8582d4 to your computer and use it in GitHub Desktop.
Using chai-as-promised library and proxyrequire to test async functions
const chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
chai.should();
const mockStoryModel = {};
const proxyquire = require('proxyquire');
//creates mock model functions to replace original model functions in controller
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;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment