Skip to content

Instantly share code, notes, and snippets.

@abrkn
Created June 3, 2012 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abrkn/2864350 to your computer and use it in GitHub Desktop.
Save abrkn/2864350 to your computer and use it in GitHub Desktop.
Mocha + Sinon + Expect.js
describe('addHandler', function() {
it('parses request and calls api', function() {
var values = {
link: 'link' + Math.floor(Math.random() * 1000),
user: 'user' + Math.floor(Math.random() * 1000),
seconds: Math.floor(Math.random() * 1000)
};
// setup
var impressionsMock = sinon.mock(impressions);
impressionsMock.expects('add', function(db, user, link, facts, callback) {
expect(db).to.be.ok();
expect(user).to.eql(values.user);
expect(link).to.eql(values.link);
expect(facts).to.be.ok();
expect(facts).to.have.property('seconds', values.seconds);
expect(typeof callback).to.be('function');
callback();
});
var res = {
end: sinon.stub()
};
var req = {
session: { user: { _id: values.user } },
body: { link: values.link, seconds: values.seconds }
};
// test
impressions.addHandler(req, res);
// verify
expect(res.end.calledOnce);
impressionsMock.verify();
impressionsMock.restore();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment