Last active
November 22, 2018 20:11
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
}); | |
}); |
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.
Nice implementation Marcus
@marcdomain nice implementation. You should, however, change the extension of the gist from .txt
to .js
Thanks for the observation
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
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?