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(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@marcdomain nice implementation. You should, however, change the extension of the gist from
.txt
to.js