Skip to content

Instantly share code, notes, and snippets.

@AwesomeZaidi
Created June 11, 2019 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AwesomeZaidi/27f2af5b4d4e1b120e18e337a2a53186 to your computer and use it in GitHub Desktop.
Save AwesomeZaidi/27f2af5b4d4e1b120e18e337a2a53186 to your computer and use it in GitHub Desktop.
Unit Testing
const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('../server');
const should = chai.should();
const Pet = require('../models/pet');
// BETTER TEST COMING SOON!
const fido = {
"name": "Norman",
"species": "Greyhound",
"birthday": "2008-11-11",
"favoriteFood": "Liver",
"picUrl": "http://www.gpamass.com/s/img/emotionheader713297504.jpg",
"picUrlSq": "https://www.collinsdictionary.com/images/thumb/greyhound_21701074_250.jpg",
"description": "Fido is a dog and he's a good dog who loves to play and hang out with his owners. He also likes to nap and enjoys eating dog food"
}
chai.use(chaiHttp);
describe('Pets', () => {
after(() => {
Pet.deleteMany({$or: [{name: 'Norman'}, {name: 'Spider'}] }).exec((err, pets) => {
console.log(pets)
pets.remove();
})
});
// TEST INDEX
it('should index ALL pets on / GET', (done) => {
chai.request(server)
.get('/')
.end((err, res) => {
res.should.have.status(200);
res.should.be.html;
done();
});
});
// TEST NEW
it('should display new form on /pets/new GET', (done) => {
chai.request(server)
.get(`/pets/new`)
.end((err, res) => {
res.should.have.status(200);
res.should.be.html
done();
});
});
// TEST CREATE
it('should create a SINGLE pet on /pets POST', (done) => {
chai.request(server)
.post('/pets')
.send(fido)
.end((err, res) => {
res.should.have.status(200);
res.should.be.html
done();
});
});
// TEST SHOW
it('should show a SINGLE pet on /pets/<id> GET', (done) => {
var pet = new Pet(fido);
pet.save((err, data) => {
chai.request(server)
.get(`/pets/${data._id}`)
.end((err, res) => {
res.should.have.status(200);
res.should.be.html
done();
});
});
});
// TEST EDIT
it('should edit a SINGLE pet on /pets/<id>/edit GET', (done) => {
var pet = new Pet(fido);
pet.save((err, data) => {
chai.request(server)
.get(`/pets/${data._id}/edit`)
.end((err, res) => {
res.should.have.status(200);
res.should.be.html
done();
});
});
});
// TEST UPDATE
it('should update a SINGLE pet on /pets/<id> PUT', (done) => {
var pet = new Pet(fido);
pet.save((err, data) => {
chai.request(server)
.put(`/pets/${data._id}?_method=PUT`)
.send({'name': 'Spider'})
.end((err, res) => {
res.should.have.status(200);
res.should.be.html
done();
});
});
});
// TEST DELETE
it('should delete a SINGLE pet on /pets/<id> DELETE', (done) => {
var pet = new Pet(fido);
pet.save((err, data) => {
chai.request(server)
.delete(`/pets/${data._id}?_method=DELETE`)
.end((err, res) => {
res.should.have.status(200);
res.should.be.html
done();
});
});
});
// SEARCH
it('should search ALL pets by name on /search GET', (done) => {
chai.request(server)
.get('/search?term=norman')
.end((err, res) => {
res.should.have.status(200);
res.should.be.html;
done();
});
});
});
@Buithienphuoc
Copy link

Hi, good example but I need your explanation about pet.save() . I'm a new learner of the Mocha Chai Test. I created 2 file tests for CRUD 2 tables with the same formula. However, 1 of them can run with the save() function and another one can't ( the pass will test if I remove the .save() line )

Hope to know your answer

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