Skip to content

Instantly share code, notes, and snippets.

@marcdomain
Last active November 22, 2018 20:11
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 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();
});
});
});
@oluwajuwon
Copy link

Hi Marcus, I like how you implemented using your mock data it's pretty cool. However, I can see you have a couple of .send statements, isn't possible to just have one and pass all the required variables in it?

@marcdomain
Copy link
Author

Thanks for the feedback. I was actually trying to test different "fail cases" at a go. The endpoint would be structured to receive all response messages in an object and send them at the same time.

@Johnsonojo
Copy link

Nice implementation Marcus

@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