gpt4 book ai didi

javascript - NodeJs、Mocha 和 Mongoose

转载 作者:数据小太阳 更新时间:2023-10-29 04:16:10 25 4
gpt4 key购买 nike

我有以下结构:

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

用他们的代码:

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: 'contact@com.br',                    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);

问题是...当我运行“mocha”来执行我的单元测试时,没有执行保存函数的回调。

谢谢大家!

最佳答案

问题已通过 this 解决方法。

我还找到了another solution看起来不错,但我没试过。

关于javascript - NodeJs、Mocha 和 Mongoose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13057016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com