2013-09-05 8 views
5

Używam pliku node.js/express z https://github.com/dresende/node-orm2 do korzystania z mojej bazy danych MySQL.W jaki sposób kpisz z MySQL (z node-orm2) w Node.js/Express?

Jestem nowy w świecie node.js i do tej pory utknąłem, nie wiem jak test jednostkowy (nie test integracji) jest prostą funkcją.

Oto moja server.js, załadunek mojego modelu użytkownika (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...'); 

Oto model użytkownika:

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(); 
}; 

i tu jest moje metody, aby znaleźć aktywnych przyjaciół przyjaciół. JS:

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

Chciałbym wiedzieć, w jaki sposób można napisać prosty test (z mokka i sinon.js?) przez mock połączenie z bazą danych i żądanie również. Muszę wyśmiać wartość parametru req.currentUser, który jest zwracany przez ORM w oprogramowaniu pośredniczącym.

Po prostu chcę uruchomić testy jednostkowe i nie używać prawdziwego DB lub wykonywać niektóre połączenia HTTP.

dzięki za pomoc.

Odpowiedz

0

Jeśli chcesz sfałszować zapytanie używając sinon.js, możesz zrobić coś takiego.

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); 
}); 

ten sposób nie powinno być podłączenie do modelu, który testuje friend.js tylko.

Ponadto, ponieważ właśnie zauważyłem, że używasz orm.express, możesz także po prostu kpić z req.models z funkcją skrótu, jak chcesz powyżej.

Powiązane problemy