如何在Node.js / Express中模拟MySQL(使用node-orm2)?

我用https://github.com/dresende/node-orm2使用node.js / express来使用我的MySQL数据库。

我是新来的node.js世界,我相当坚持到目前为止,我不知道如何unit testing(而不是集成testing)一个简单的function。

这里是我的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...'); 

这里是用户模型:

 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, required: true }, last_name : { type: 'text', size: 100, required: true }, picture : { type: 'text', size: 255, required: false }, email : { type: 'text', size: 255, required: true }, 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中find活跃朋友的方法:

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

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

我只想运行unit testing,不要使用真正的数据库或进行一些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 (ie 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); }); 

这样你就不应该连接到只testingfriend.js的模型。

另外,因为我刚刚注意到你正在使用orm.express ,所以你可能也想简单地用上面想要的req.models函数来模拟req.models