Full-Stack Web Development with Vue.js and Node

(singke) #1
Testing an MEVN Application Chapter 9

Since this method fetches all the existing movies from the database, we will first need to
build the mock movies here to actually test it. Let's replace the contents of


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(){
expect(controller).to.exist
})

describe('/GET movies', () => {
it('it should send all movies', (done) => {
var movie1 = {
name: 'test1',
description: 'test1',
release_year: 2017,
genre: 'test1'
};
var movie2 = {
name: 'test2',
description: 'test2',
release_year: 2018,
genre: 'test2'
};
var expectedMovies = [movie1, movie2];
sinon.mock(Movie)
.expects('find')
.yields('', expectedMovies);
chai.request(server)
.get('/movies')
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.an('object');
expect(res.body).to.eql({
movies: expectedMovies
});
done();
});
Free download pdf