Last active
March 13, 2019 16:40
-
-
Save mystere10/9bd7675e4216d43e344c3010e617c9e7 to your computer and use it in GitHub Desktop.
Comment for test
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 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
commented
Mar 13, 2019
- Image should be a filename
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
}
}
}
Wonderful job!!
Here are some few things to change:
- use
chai.should()
only instead ofconst should = chai.should();
- clearly, name each test.
- correct some syntax error before chai.request(server) line.
- you don't need this line
clientToken = res.body.token;
because you will get this token from another test of login endpoint.
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