Full-Stack Web Development with Vue.js and Node

(singke) #1
Testing an MEVN Application Chapter 9

This controller has two methods—one GET request and a POST request. The GET request is


for fetching all the movies from the database, and the POST request saves the movies with


the given parameters to the database.


Let's move on to adding the spec for the GET request first. Add the following contents in


the movies.spec.js file that we just created:


const controller = require("./../../../../controllers/movies.js");
const Movie = require("./../../../../models/Movie.js");
let server = require('./../../../../server.js');
let chai = require('chai');
let sinon = require('sinon');
const expect = chai.expect;
let chaiHttp = require('chai-http');
chai.use(chaiHttp);
const should = chai.should();

The first two lines required the corresponding controller and model for the Movie


component, which we will need later. We will also require the server file.


The other packages, such as chai, sinon, expect, and should, are needed for the


assertions.


The next thing that we will need to make requests to the server is a package called chai-


http. This package will be used for HTTP request assertions. So, let's install this package


first with the following command:


$ npm install chai-http --save

Now, we can get ahead with adding the first test. Replace the contents in movies.spec.js


with the following code:


const controller = require("./../../../../controllers/movies.js");
const Movie = require("./../../../../models/Movie.js");
let server = require('./../../../../server.js');
let chai = require('chai');
let sinon = require('sinon');
const expect = chai.expect;
let chaiHttp = require('chai-http');
chai.use(chaiHttp);
const should = chai.should();

describe('controllers.movies', function(){
it('exists', function(){
Free download pdf