Testing a model

When testing our models, we want to include the model module itself and then write tests against it. The easiest solution here is that we create a test model object and then assert that the model has all of the fields that we expect, as well as any virtuals we might have created.

Create the tests/models/image.test.js file and insert the following code:

let ImageModel = require('../../models/image'); 

describe('Image Model',()=>{
var image;

it('should have a mongoose schema',()=>{
expect(ImageModel.schema).to.be.defined;
});

beforeEach(()=>{
image = new ImageModel({
title: 'Test',
description: 'Testing',
filename: 'testfile.jpg'
});
});

// to do: write tests...
});

First, we include ImageModel using require (note the path for the require statement). The very first test we run is to make sure that ImageModel has a mongoose schema property. After this test, we define the beforeEach block that we'll rely on for the remainder of our tests. Before every test, we will want to instantiate a new ImageModel object that we can test against. We can do this in a beforeEach block so that we're sure that we're dealing with a fresh object in every test and that it hasn't been tainted by any tests that were previously run. It's also important to note that the order of the first test and the beforeEach block doesn't actually matter, as the beforeEach block will run before every test in its parent describe function, regardless of the order it was defined in.

Include the following suite of tests replacing the placeholder // to do: write tests... comment:

describe('Schema', ()=>{
it('should have a title string', ()=>{
expect(image.title).to.be.defined;
});
it('should have a description string', ()=>{
expect(image.description).to.be.defined;
});
it('should have a filename string', ()=>{
expect(image.filename).to.be.defined;
});
it('should have a views number default to 0', ()=>{
expect(image.views).to.be.defined;
expect(image.views).to.equal(0);
});
it('should have a likes number default to 0', ()=>{
expect(image.likes).to.be.defined;
expect(image.likes).to.equal(0);
});
it('should have a timestamp date', ()=>{
expect(image.timestamp).to.be.defined;
});
});

Here, we will check to ensure that each property that we expect an ImageModel instance to have is defined. For the properties that have default values set, we also check to ensure that the default values are set as well.

Next, we will test against the virtuals that we expect ImageModel to have, and verify that they function the way they're supposed to:

describe('Virtuals', ()=>{
describe('uniqueId', ()=>{
it('should be defined', ()=>{
expect(image.uniqueId).to.be.defined;
});
it('should get filename without extension', ()=>{
expect(image.uniqueId).to.equal('testfile');
});
});
});

When testing the uniqueId virtual, it should return the image model's filename without the extension. As the beforeEach defines our image model with a filename of testfile.jpg, we can assert with our test that the uniqueId returned is equal to testfile (the filename without the extension).

Running the tests for our model should provide the following results:

    $ npm test
Image Model
should have a mongoose schema
Schema
should have a title string
should have a description string
should have a filename string
should have a views number default to 0
should have a likes number default to 0
should have a timestamp date
Virtuals
uniqueId
should be defined
should get filename without extension
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.99