Full-Stack Web Development with Vue.js and Node

(singke) #1
Testing an MEVN Application Chapter 9

});
});
})

Let's learn step by step what we did here:


We created a couple of movies with sinon mocks
We created an HTTP GET request using chai
We had three expectations:
The status of the request should be 200
The request response should be an object
The response should contain the list of movies that we created
with the mock

Let's run the test again with the following command:


$ mocha test/unit/specs/controllers/movies.spec.js

The tests should pass.


Let's now move on to add the tests for the POST request for movies.js. In movies.js,


here is what we have so far:


var Movie = require("../models/Movie");

module.exports.controller = (app) => {
...

// add a new movie
app.post('/movies', (req, res) => {
const movie = new Movie({
name: req.body.name,
description: req.body.description,
release_year: req.body.release_year,
genre: req.body.genre
})

movie.save(function (error, movie) {
if (error) { console.log(error); }
res.send(movie)
})
})
}
Free download pdf