Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了你如何在Node.js / Express中模拟MySQL(带node-orm2)?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
使用Node.js / express和 https://github.com/dresende/node-orm2来使用我的MysqL数据库.

我是node.js世界的新手,到目前为止我很卡住,我不知道如何进行单元测试(不是集成测试)一个简单的函数.

这是我的server.js,加载我的用户模型(ORM)

var express = require('express'),orm = require('orm'),config = require('./config/config.js'),auth = require('./services/authentication'),Helper = require('./middlewares/Helper.js'),friends = require('./modules/friends.js');

var app = express();

app.use(orm.express('MysqL://' + config.MysqL.username + ':' + config.MysqL.pwd + '@' + config.MysqL.host + ':' + config.MysqL.port + '/' + config.MysqL.db,{
    define: function(db,models,next) {
        db.load("./models/models.js",function(err) { // loaded!
            modelS.User = db.modelS.User;
        });
    }
}));

var middlewares = [auth.authenticate,Helper.retrieveUser];

app.get('/friends',middlewares,friends.findActiveFriends);

app.listen(3000);
console.log('Listening on port 3000...');

这是用户模型:

@H_613_11@module.exports = function (db,cb) { var User = db.define('user',{ uid : { type: 'number',rational: false,unique: true,required: true },first_name : { type: 'text',size: 100,last_name : { type: 'text',picture : { type: 'text',size: 255,required: false },email : { type: 'text',creation_date : { type: 'date',time: true },modification_date : { type: 'date',time: true } },{ methods: { fullName: function () { return this.first_name + ' ' + this.last_name; } },hooks: { beforeCreate: function (next) { if (this.creation_date == undefined) { this.creation_date = new Date(); } if (this.modification_date == undefined) { this.modification_date = new Date(); } return next(); } } }); // CUSTOM FUNCTIONS User.getByUid = function(uid,callBACk) { this.find({ uid: uid },function(err,users) { if(err) callBACk(err); if (users.length == 1) { callBACk(null,users[0]); } else { callBACk('No user found with uid=' + uid); } }); }; User.hasmany("friends",User,{ status: { type: 'enum',values: ['pending','refused','active'] } },{ reverse: 'friendsrev',mergEID: 'user_id',mergeAssocId: 'friend_id' }); return cb(); };

这是我在friends.js找到活跃朋友的方法

var _findActiveFriends = function(req,res) {
    req.currentUser.getFriends({
        status: 'active'
    },friends) {
        if (err) throw err;
        res.send(JSON.Stringify(friends));
    });
};

我想知道如何通过模拟数据库连接和请求来编写一个简单的测试(使用mocha和sinon.js?).我需要模拟req.currentUser的值,它是ORM在中间件中返回的用户.

我只想运行单元测试,不要使用真正的数据库或进行一些http调用.

谢谢你的帮助.

解决方法

如果你想使用sinon.js模拟req,你可以执行以下操作.

var sinon = require('sinon');
var friend = require('./friend');

it('some test',function(donE) {
  var req = {
    currentUser: {
      // Add all the properties/functions that you are concerned with here. 
      // and you can/should wrap them around sinon.spy().
      // If you are not concerned with that function (i.e. you are not using it) 
      // then you can simply use sinon.stub() to return a stub function. 
    }
  };
  var res = {
    send: sinon.spy(function(obj) { 
      assert.ok(obj); // yes object exists 
      done(); // end of test
    };
  };
  var next = function() {};
  friend.findActiveFriend(req,res,next);
});

这样您就不应该连接到仅测试friend.js的模型.

此外,由于我刚刚注意到您正在使用orm.express,您可能还想简单地使用您想要的存根函数模拟req.models.

大佬总结

以上是大佬教程为你收集整理的你如何在Node.js / Express中模拟MySQL(带node-orm2)?全部内容,希望文章能够帮你解决你如何在Node.js / Express中模拟MySQL(带node-orm2)?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。