Full-Stack Web Development with Vue.js and Node

(singke) #1
Testing an MEVN Application Chapter 9

expect(controller).to.exist
})
})

The preceding method describes the movies controller. It simply checks whether the


controller we are describing exists or not.


To make sure we have the connection of our node server, let's export the server from


server.js. Add the following code into server.js:


...
const port = process.env.API_PORT || 8081;
app.use('/', router);
var server = app.listen(port, function() {
console.log(`api running on port ${port}`);
});

module.exports = server

Now, let's run the test using the following command:


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

The test should pass.


Let's move on to adding the test for the GET request. In movies.js, we have the following


code:


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

module.exports.controller = (app) => {
// fetch all movies
app.get("/movies", function(req, res) {
Movie.find({}, 'name description release_year genre', function
(error, movies) {
if (error) { console.log(error); }
res.send({
movies: movies
})
})
})
...
}
Free download pdf