Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- process.env.NODE_ENV = 'test';
- let mongoose = require('mongoose');
- let Book = require('../models/book');
- // les dependencies pour le dev
- /*
- Avoir chai
- Avoir chai http
- Avoir should
- */
- chai = require('chai')
- chaiHttp = require('chai-http')
- server = require('../app')
- should = chai.should();
- chai.use(chaiHttp);
- describe('Books', () => {
- beforeEach((done) => { // Avant chaque test tu vas vider la database
- Book.remove({}, (err) => {
- done()
- })
- })
- describe('/GET book', () => {
- it('should GET return all books', (done) => {
- chai.request(server)
- .get('/book')
- .end((err, res) => {
- res.should.have.status(200);
- res.body.should.be.a('array');
- res.body.length.should.be.eql(0);
- done();
- });
- });
- });
- describe('/POST book' , () => {
- it('it should not POST a book without pages field', (done) => {
- let book = {
- title: "The Lord of the Rings",
- author: "J.R.R. Tolkien",
- year: 1954
- }
- chai.request(server)
- .post('/book')
- .send(book)
- .end((err, res) => {
- res.should.have.status(200);
- res.body.should.be.a('object');
- res.body.should.have.property('errors');
- res.body.errors.should.have.property('pages');
- res.body.errors.pages.should.have.property('kind').eql('required');
- done();
- });
- });
- it('should POST a book', (done) => {
- let book = {
- title: "The Lord of the Rings",
- author: "J.R.R. Tolkien",
- year: 1954,
- pages: 1170
- }
- chai.request(server)
- .post('/book')
- .send(book)
- .end((err, res) => {
- res.should.have.status(200);
- res.body.should.be.a('object');
- res.body.should.have.property('message').eql('Book successfully added!');
- res.body.book.should.have.property('title');
- res.body.book.should.have.property('author');
- res.body.book.should.have.property('pages');
- res.body.book.should.have.property('year');
- done();
- })
- })
- })
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement