Skip to content

Instantly share code, notes, and snippets.

@musonant
Last active March 20, 2019 00:56
Show Gist options
  • Select an option

  • Save musonant/16a658223e016e2379286cba295923a7 to your computer and use it in GitHub Desktop.

Select an option

Save musonant/16a658223e016e2379286cba295923a7 to your computer and use it in GitHub Desktop.
import chai, { expect } from 'chai';
import { describe, it } from 'mocha';
import chaiHttp from 'chai-http';
chai.use(chaiHttp);
chai.should();
const newArticle = {
title: 'Design patterns',
body: 'There are several design patterns e.g: MVC pattern',
description: 'List of most popular design patterns used in Javascript'
};
describe('Test cases for Authors Haven app', () => {
it('POST /articles/', (done) => {
chai.request(app)
.post('/api/articles')
.send(newArticle)
.end((err, res) => {
res.should.have.status(201);
res.body.should.be.a('object');
res.body.article.should.be.a('object');
res.body.article.id.should.be.a('number');
res.body.article.title.should.equal('Design patterns');
res.body.article.body.should.equal('There are several design patterns e.g: MVC pattern');
res.body.article.body.should.equal('List of most popular design patterns used in Javascript');
done();
});
});
})
@igbominadeveloper
Copy link

Instead of hardcoding the expected values like you have done, to be on the safe side, you could use a reference to the original data you sent to the server i.e. res.body.article.title.should.equal('Design patterns') could be rewritten as res.body.article.title.should.equal(newArticle.title). This assertion as you have written it would fail if a single space or a character is mistakenly included in the hardcoded strings.
But the code is neatly written and clear

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