Full-Stack Web Development with Vue.js and Node

(singke) #1
Testing an MEVN Application Chapter 9

Let's create a new file, called movies.spec.js, inside the test/units folder:


var movies = require("./../../../controllers/movies.js");
var expect = require('chai').expect;

describe('controllers.movies.js', function(){
it('exists', function(){
expect(movies).to.exist
})
})

This test code simply checks whether the controller exists or not, which should pass


when we run the following command:


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

This command runs the tests for our controller/movies.js and should pass with the


following output:


Let's first write a test for a simple method. Let's create a request that responds with just an


object with a name. In movies.js, let's add the following code to create a dummy API:


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

module.exports.controller = (app) => {
// send a dummy test
app.get("/dummy_test", function(req, res) {
res.send({
name: 'John'
})
})

In the preceding code, we have a simple method that returns an object.

Free download pdf