Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJs, Mocha and Mongoose

I have the follow structure:

|server
 |db
  |mongooseTest.js
 |test
  |userModel.test.js
 |user
  |userModel.js

With their code:

mongooseTest.js


    var mongoose = require('mongoose');

    module.exports = function() {
        var db = mongoose.createConnection('localhost', 'dbUnitTest');

        db.on('connected', function() {
            console.log('DB: ' + db.name + ' local: ' + db.host + ':' + db.port);
        });

        db.on('error', function(err) {
            console.log(err);
        });

        return db;
    };

userModel.test.js


    var assert = require('assert'),
        should = require('should'),
        conn = require('../db/mongooseTest'),
        UserModel = require('../user/userModel');

    describe('User Model', function() {
        describe('Save', function() {

            it('Saving...', function() {
                var db = conn();
                var Model = db.model('User');
                var userModel = new Model({
                    name: 'My Name',
                    email: '[email protected]',
                    pass: 'anything123'
                });

                userModel.on('save', function(user) {
                    console.log('Passed by save event handle from user');
                });

                userModel.save(function(err, user) {
                    console.log('Passed by save from user');
                    if(err) console.log(err);
                    console.log(user);
                });
            });
        })
    })

userModel.js


    var mongoose = require('mongoose'),
        crypto = require('crypto'),
        Schema = mongoose.Schema;

    var setPass = function(value) {
            var salt = 'anyRandomSaltValue';
            this.set('salt', salt);
            var pass = hashPass(value + salt);
            return pass;
        }

    var hashPass = function(value) {
            return crypto.createHash('sha1').update(value).digest('HEX');
        }

    var userSchema = new Schema({
        name: {
            type: String,
            required: true
        },
        email: {
            type: String,
            required: true,
            unique: true
        },
        pass: {
            type: String,
            required: true,
            set: setPass
        },
        salt: {
            type: String,
            required: true
        }
    });

    userSchema.method({
        validatePass: function(senha) {
            var salt = this.get('salt');
            var passSha = hashPass(senha + salt);
            return passSha === this.get('senha');
        },
    });

    userSchema.static({
        findByEmail: function(email, success, error) {
            this.findOne({
                email: email
            }, function(e, o) {
                if(e) {
                    if(error) error(e);
                } else {
                    if(success) success(o);
                }
            });
        },
    });

    module.exports = mongoose.model("User", userSchema);

The problem is... When I run "mocha" to execute my unit tests, the callback of the save function is not performed.

Thanks for all!

like image 234
Anderson Luiz Nogueira Avatar asked Oct 24 '12 20:10

Anderson Luiz Nogueira


People also ask

What is Mocha Mongoose?

npm install --save mocha nodemon mongoose. Mongoose is a library which helps to modify the database. Mocha testing framework is popular for testing Nodejs and we are going to test create, read, update and delete operations on our MongoDB database.

What is NodeJS mocha?

Mocha is a widely used JavaScript test framework running on NodeJS and browsers. It supports asynchronous testing running the tests serially, allowing for more flexible and accurate reporting. It is a highly customizable framework that supports different assertions and libraries.

Which is better jest or mocha?

Jest is also faster than Mocha. It has built-in support for snapshot testing, which means tests are run automatically on each change to the code. This makes it easy to keep your tests up to date as you work. Mocha has more features out of the box since it is a more mature tool with a larger community of contributors.

What is Mongoose in NodeJS?

Mongoose is a Node.js-based Object Data Modeling (ODM) library for MongoDB. It is akin to an Object Relational Mapper (ORM) such as. SQLAlchemy. for traditional SQL databases. The problem that Mongoose aims to solve is allowing developers to enforce a specific schema at the application layer.


1 Answers

Issue solved with the this approach.

I also found another solution which looks good but I didn'try.

like image 115
Anderson Luiz Nogueira Avatar answered Sep 21 '22 18:09

Anderson Luiz Nogueira