Skip to content

Instantly share code, notes, and snippets.

@emilong
Last active March 29, 2016 18:52
Show Gist options
  • Save emilong/ed02d460bf236914d8af to your computer and use it in GitHub Desktop.
Save emilong/ed02d460bf236914d8af to your computer and use it in GitHub Desktop.
const chai = require('chai');
chai.use(require('sinon-chai'));
const sinon = require('sinon');
describe('controller.Pdfs', () => {
var bottle, generate, res;
beforeEach(() => {
res = {
json: sinon.spy()
};
bottle = new Bottle();
// Load the subject under test into the unit container.
// Note the path here relative is to NODE_PATH, which we've set to the root of the project.
require('./app/controllers/pdf');
});
context('when the PdfGenerator returns successfully', () => {
beforeEach(() => {
// lazily declare our dependencies just for the block of tests for which
// the setup is relevant.
bottle.service('service.PdfGenerator', function createFakePdfGenerator() {
generate = sinon.spy(() => 123);
return generate;
});
// call the subject under test from the container.
bottle.container.controller.Pdf.create({ params: { content_id: 456 } }, res);
});
it('invokes generate', () => {
expect(generate).to.have.been.calledWith(456);
});
it('returns the pdf_id as json', () => {
expect(res.json).to.have.been.calledWith({ pdf_id: 123 });
});
});
context('when the PdfGenerator throws an error', () => {
beforeEach(() => {
// load alternative implementations for different scenario types.
bottle.service('service.PdfGenerator', function createFakePdfGenerator() {
generate = sinon.spy(() => {
throw new Error('it all went wrong');
});
return generate;
});
});
// ...
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment