Mongoose Jasminejs单位测试方法(DAO)

时间:2014-01-16 14:45:37

标签: mongodb unit-testing angularjs jasmine

我正在构建一个Nodejs + Mongoose + MongoDB Web应用程序,我正在尝试使用Jasmine单元测试框架对我的DAO方法进行单元测试。

我的模型如下:

var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var profilSchema = new Schema({
photo: {type: String},
nom: {type: String , required: true},
type: { type: String , required: true},
descriptif: {type: String, required: true},
niveauScolaire: {type: String , required: true},

});

/**
* Statics
*/
 profilSchema.statics = {
load: function(id, cb) {
    this.findOne({
        _id: id
    }).exec(cb);
}
  };

  var Profil = mongoose.model('Profil', profilSchema);

在负责显示所有配置文件的DAO功能中,我有以下代码

/**
 * List of Profiles
 */
  exports.all = function(req, res) {
   Profil.find().exec(function(err, profils) {
    if (err) {
        res.render('error', {
            status: 500
        });
    } else {
        // console.log(profils._id);
        res.jsonp(profils);
    }
});
};

在我的控制器中我调用了这样一个函数:

    $scope.afficherProfils = function() {
    $http.get('/listerProfil')
        .success(function(data) {
        $scope.listeProfils = data;

    });

};

我如何测试这种功能?是否可以在mongoDB数据库中插入一些元素并测试函数是否获取信息? 我真的没有这种单元测试的经验。

1 个答案:

答案 0 :(得分:1)

您必须单独测试服务器端和客户端功能。

Angularjs位将在浏览器中运行。您可以使用karmatestem来运行客户端测试。您不需要在测试中发出实际的http请求。您可以$httpBackend来控制行为。

看看如何编写茉莉花测试并深入了解angularjs文档,以便更好地理解为angularjs编写单元和e2e测试

进一步阅读材料:

http://www.yearofmoo.com/2013/01/full-spectrum-testing-with-angularjs-and-karma.html http://www.yearofmoo.com/2013/09/advanced-testing-and-debugging-in-angularjs.html

node.js位将在服务器端运行,您可以使用mocha运行服务器端测试。许多项目使用单独的测试数据库进行测试,在测试套件运行之前用测试数据填充测试数据,然后在测试运行完成后清理数据库。

相关问题