Skip to content

Instantly share code, notes, and snippets.

@DannyDouglass
Created August 23, 2013 19:59
Show Gist options
  • Save DannyDouglass/6323369 to your computer and use it in GitHub Desktop.
Save DannyDouglass/6323369 to your computer and use it in GitHub Desktop.
Simple api mock test against github enterprise api to create a new repository
describe('POST to Github API to create new repository with answers', function() {
it('should return an HTTP Status 201 Completed', function() {
var fakeAnswers = {
githubRepoName: 'test-repo',
githubOrganization: 'test-organization'
};
var githubMock = nock('http://github.nreca.org')
.post('/api/v3/orgs/' + fakeAnswers.githubOrganization + '/repos', fakeAnswers)
.reply(201, 'POST was successful');
var options = {
uri: "http://github.nreca.org/api/v3/orgs/" + fakeAnswers.githubOrganization + "/repos",
method: "POST",
json: fakeAnswers
};
request.post(options, function(error, response, body) {
githubMock.done();
assert(response.statusCode === 201)
});
});
});
@scarney81
Copy link

Line 2: function(done) instead of function(). Done will allow you to indicate you are done with asynchronous processing - which you have here.

Line 21: below this call done() - this will indicate you've finished async

Line 0: require('should') - so we can easily test some assertions

Line 21: response.statusCode.should.be(201) - actual test verification

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment