Skip to content

Instantly share code, notes, and snippets.

@marcdomain
Last active November 22, 2018 20:11
Show Gist options
  • Save marcdomain/4fe3bbf81a59c8830c7951207bcd5d7b to your computer and use it in GitHub Desktop.
Save marcdomain/4fe3bbf81a59c8830c7951207bcd5d7b to your computer and use it in GitHub Desktop.
This test is for a post REST API Endpoint. I assumed a mock data that hold objects of correct and incorrect input from a client. The response code is tested in each case. I ensured that the test captures the actual purpose of the endpoint (a push into the Articles model), by testing the new length of the Articles Model.
import chai from 'chai';
import chaiHttp from 'chai-http';
import app from '../../app';
Import articles from ‘../Models/articles’;
Import { correctArticle, invalidArticleTitle, invalidArticleTitleLength,
invalidArticleContent, invalidArticleLength
} from ‘./articleMockData’;
describe(‘TEST FOR POST Article to the Articles model’, () => {
it(‘should return 201 for success’, (done) => {
const updatedArticleLength = articles.length + 1;
chai.request(app)
.post(‘/api/v1/articles’)
.send(correctArticle)
.end((error, response) => {
expect(response).to.have.status(201);
expect(articles).to.have.length(updatedArticleLength);
expect(response.body.message).to.equal(‘Article posted successfully!’);
done();
})
})
it('Should return failed status for incorrect input', (done) => {
chai.request(app)
.post('/api/v1/articles')
.send(invalidArticleTitle)
done()
.send(invalidArticleTitleLength)
done()
.send(invalidArticleContent)
done()
.send(invalidArticleLength)
done()
.end((error, response) => {
expect(response).to.have.status(400);
expect(response.body.status).to.equal(‘Fail’);
done();
});
});
});
@luqmanoop
Copy link

@marcdomain nice implementation. You should, however, change the extension of the gist from .txt to .js

@marcdomain
Copy link
Author

Thanks for the observation

@akhilome
Copy link

Phenomenal execution, @marcdomain. All grounds look covered to me. 💪

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