dimanche 18 septembre 2016

How to test node.js-mongodb app using Mocha, Chai and Sinon? I am having difficulty with Sinon part

I am new to testing and having difficulty with Sinon stubs and mocks.

Here is the routes file : quotes.js

const express = require('express');
const request = require('request');
const async = require('async');
const validator = require('validator');

const quote_router = express.Router();

const confg = require("../../confg/confg");
const quote = require("../models/mquotes");
const quotes_model = quote.quotes;

// host name - needs to be set up using the environment variable
const hostname = confg.hostname;

// route for "quotes/"
quote_router.route("/")
// get route : display the random quote
.get((req, res) => {
    // display random quote
    async.waterfall([
            (callback) => {callback(null, {res});},
            quote.count_quotes
        ], check_quote_exist
    );
})
// post route : create a new quote
.post((req, res) => {
    const doc_json = {author : validator.escape(req.body.quote_author), quote_text : validator.escape(req.body.quote_text)};
    const params = {res, doc_json, quote_action : quote.create_quote};
    add_edit_quote(params);
})
// put route : edit the quote
.put((req, res) => {
    const doc_json = {author : validator.escape(req.body.quote_author), quote_text : validator.escape(req.body.quote_text)};
    const params = {res, doc_json, quote_action : quote.update_quote, qid : req.body.quote_id};
    add_edit_quote(params);
})
// delete quote : delete the quote
.delete((req, res) => {
    const qid = req.body.qid;
    const condition = {_id : qid};
    async.waterfall([
            (callback) => {callback(null, {res, condition});},
            quote.delete_quote
        ], request_quote_list
    );
});

// route for "quotes/list" : display quotes list
quote_router.get("/list/", (req, res) => {
    // mention the main operation 
    let operation;

    if(req.body.operation != 'undefined') {
        operation = req.body.operation;
    } else {
        operation = "list_quotes";
    }

    async.waterfall([
            (callback) => {callback(null, {res, operation});},
            quote.list_quote
        ], display_quotes_list
    );
});
// display the quotes list
const display_quotes_list = (err, params, quotes_list) => {
    if (err) {return console.log(err);}

    const res = params.res;
    const operation = params.operation;
    const header_msg = "List of all the quotes";
    let alert_msg;

    if(operation == "list_quotes")  {
        alert_msg = null;
    } else if(operation == "delete_quote")  {
        alert_msg = "Quote has been deleted";
    }

    const params_out = {
        page: "quote_list",
        title: 'Quotes Manager',
        host: hostname,
        header_msg,
        alert_msg,
        quotes_list
    };
    res.render('index', params_out);
};

// send http request for quote list page
const request_quote_list = (err, params) => {
    if (err) {return console.log(err);}

    const res = params.res;
    const operation = "delete_quote";

    request.get('http://' + hostname + '/quotes/list/', {json:{operation}},
        (error, response, body) => {
            if (!error && response.statusCode == 200) {
                res.send(body);
            }
    });
};

module.exports = quote_router;

This is not complete file. I have included only a portion of it.

And her is the model file : mquotes.js

const mongoose = require('mongoose');

// Define quote schema
const quoteSchema = new mongoose.Schema({
    author: String,
    quote_text: {type: String, required: true}
    },
    {timestamps: true}
);
const quote = {};

// Define quotes model
quote.quotes = mongoose.model('quotes', quoteSchema);

// error handler 
error_handler = (callback, err, params, return_value) => {
    if(err) { return callback(err);}
    else {callback(null, params, return_value);}
};

// add quote - create
quote.create_quote = (params, callback) => {
    const res = params.res;
    const doc_json = params.doc_json;
    quote.quotes.create(doc_json, (err, quotes_detail) => {
        error_handler(callback, err, {res, operation : 'create_quote'}, quotes_detail);
    });
};

// count the number of quotes
quote.count_quotes = (params, callback) => {
    quote.quotes.count({}, (err, quotes_count) => {
        error_handler(callback, err, params, quotes_count);
    });
};

// delete quote - delete - id
quote.delete_quote = (params, callback) => {
    quote.quotes.remove(params.condition, (err, query) => {
        error_handler(callback, err, params);
    });
};

// list quote - find
quote.list_quote = (params, callback) => {
    quote.quotes.find({}, (err, quotes_list) => {
        error_handler(callback, err, params, quotes_list);
    });
};

// find quote by id
quote.quote_by_id = (params, callback) => {
    quote.quotes.findById(params.qid, (err, quotes_detail) => {
        error_handler(callback, err, params, quotes_detail);
    });
};

// returns the detail of random quote
quote.random_qoute = (params, callback) => {
    const random_number = params.random_number;

    // select one quote after skipping random_number of times
    quote.quotes.findOne({}, (err, quotes_detail) => {
        error_handler(callback, err, params, quotes_detail);
    }).skip(random_number);
};

// update quote - update - id
quote.update_quote = (params, callback) => {
    const options = {new: true};
    const qid = params.qid;
    const update_json = params.doc_json;

    quote.quotes.findByIdAndUpdate(qid, {$set: update_json}, options, (err, quotes_detail) => {
        params.operation = 'update_quote';
        error_handler(callback, err, params, quotes_detail);
    });
};

module.exports = quote;

I have installed mocha globally. Now, I want to test the model. Lets take the quote.list_quote function for example.

const mongoose = require('mongoose');
const chai = require('chai');
const sinon = require('sinon');

const expect = chai.expect; // use the "expect" style of Chai
const mquotes = require('./../../app/models/mquotes');

describe('Tests for quote models', () => {
    describe("List quote", () => {
        it('list_quote() should return list of quotes', () => {

        });
    });
});

Can anyone tell me about my coding practice too. I mean the way I use functions and modules.

Aucun commentaire:

Enregistrer un commentaire