You can use server.inject() with Mocha. I will just close Post.save() :
Sinon.stub(Post, 'save', function (callback) { callback(null, { foo: 'bar' }); });
With one more code:
it('creates a post', function (done) { Sinon.stub(Post, 'save', function (callback) { callback(null, { foo: 'bar' }); }); server.inject({ method: 'POST', url: '/posts', payload: { foo: 'bar' } }, function (res) { Post.save.restore(); expect(res.statusCode).to.equal(201); done(); }); });
If you want to test the error, you just need to change the stub:
it('returns an error when save fails', function (done) { Sinon.stub(Post, 'save', function (callback) { callback(new Error('test'), null); }); server.inject({ method: 'POST', url: '/posts', payload: { foo: 'bar' } }, function (res) { Post.save.restore(); expect(res.statusCode).to.equal(500); done(); }); });
source share