Full-Stack Web Development with Vue.js and Node

(singke) #1
Testing an MEVN Application Chapter 9

Let's use chai in our previous test. In add.spec.js, add the following lines of code:


var expect = require('chai').expect;
var addUtility = require("./../add.js");

describe('Add', function(){
describe('addUtility', function(){
it('should have a sum method', function(){
expect(addUtility).to.be.an('object');
expect(addUtility).to.have.property('sum');
})

it('addUtility.sum(5, 4) should return 9', function(){
expect(addUtility.sum(5, 4)).to.deep.equal(9);
})

it('addUtility.sum(100, 6) should return 106', function(){
expect(addUtility.sum(100, 6)).to.deep.equal(106);
})
})
});

We have replaced the assertion library with, chai expect() method, which makes the


code very much simpler and understandable.


Introducing sinon


sinon is used to test spies, stubs, and mocks for JavaScript tests. To learn about these, let's


move on to the movie rating application we have in our controller file,


controller/movies.js:


const Movie = require("../models/Movie");
const passport = require("passport");

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