Full-Stack Web Development with Vue.js and Node

(singke) #1
Testing an MEVN Application Chapter 9

Writing tests for models


Let's move on to adding the tests for the models that we have defined. Let's create a folder


called models inside test/unit/specs and create a test file for our Movie.js model.


So, the name of the spec file would be Movie.spec.js.


Let's first take a look at what we have in our Movie.js:


const mongoose = require('mongoose');
const Schema = mongoose.Schema
const MovieSchema = new Schema({
name: String,
description: String,
release_year: Number,
genre: String
})

const Movie = mongoose.model('Movie', MovieSchema)
module.exports = Movie

We just have a Schema defined here, which defines the data type for the Movie collection.


Let's add the specs for this model. Add the following contents to the Movie.spec.js:


var Movie = require("./../../../../models/Movie.js");
let chai = require('chai');
var expect = chai.expect;
var should = chai.should();

We do not need all the components that we added to the controller test here. We just have


simple assertion tests here, so we will need the Movie model and the chai methods.


Let's add the test for the Movie existence just like we did for the controller. Replace the


contents in Movie.spec.js with the following code:


var Movie = require("./../../../../models/Movie.js");
let chai = require('chai');
var expect = chai.expect;
var should = chai.should();

describe('models.Movie', function(){
it('exists', function(){
expect(Movie).to.exist
})
})
Free download pdf