Skip to content

Instantly share code, notes, and snippets.

@mystere10
Last active March 13, 2019 16:40
Show Gist options
  • Save mystere10/9bd7675e4216d43e344c3010e617c9e7 to your computer and use it in GitHub Desktop.
Save mystere10/9bd7675e4216d43e344c3010e617c9e7 to your computer and use it in GitHub Desktop.
Comment for test
import chai from 'chai';
import chaiHttp from 'chai-http';
import server from '../index.js';
chai.should();
chai.use(chaiHttp);
let clinentToken;
describe('Tests for comments', () => {
it('should login ', (done) => {
const cred = {
username: 'clet',
password: '1234',
};
chai.request(server)
.post('/api/v1/auth/login')
.send(cred)
.then((res) => {
clientToken = res.body.token;
});
});
it('should create a comment', (done) => {
const comment = {
body: 'It takes a Jacobian',
};
chai.request(server)
.post('/api/articles/1/comments')
.send(comment)
.set('Authorization', `Bearer, ${clientToken}`)
.end((err, res) => {
res.should.have.status(201);
res.body.should.be.a('object');
res.body.should.have.property('comment');
res.body.comment.should.have.property('createdAt').eql('2016-02-18T03:22:56.637Z');
res.body.comment.should.have.property('updatedAt').eql('2016-02-18T03:22:56.637Z');
res.body.comment.should.have.property('body').eql('It takes a Jacobian');
res.body.comment.author.should.be.a('object');
res.body.comment.author.should.have.property('username').eql('It takes a Jacobian');
res.body.comment.author.should.have.property('bio').eql('jake');
res.body.comment.author.should.have.property('image').eql('https://i.stack.imgur.com/xHWG8.jpg');
res.body.comment.author.should.have.property('following').eql(false);
done(err);
});
});
});
@mwibutsa
Copy link

  • Image should be a filename

@jnkindi
Copy link

jnkindi commented Mar 13, 2019

You should use this format

{
"comment": {
"id": 1,
"createdAt": "2016-02-18T03:22:56.637Z",
"updatedAt": "2016-02-18T03:22:56.637Z",
"body": "It takes a Jacobian",
"author": {
"username": "jake",
"bio": "I work at statefarm",
"image": "https://i.stack.imgur.com/xHWG8.jpg",
"following": false
}
}
}

@Inclet
Copy link

Inclet commented Mar 13, 2019

Wonderful job!!

Here are some few things to change:

  1. use chai.should() only instead of const should = chai.should();
  2. clearly, name each test.
  3. correct some syntax error before chai.request(server) line.
  4. you don't need this line clientToken = res.body.token; because you will get this token from another test of login endpoint.

@jnkindi
Copy link

jnkindi commented Mar 13, 2019

You should test values in the sent variable not rewrite the value but reference the variable values.

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