Last active
October 10, 2017 13:41
-
-
Save isabellachen/fcffbf2b3567a0299a3a06bd2f8582d4 to your computer and use it in GitHub Desktop.
Using chai-as-promised library and proxyrequire to test async functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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