I have a User
sequelize model that's loaded using the index.js
file in the models directory e.g.
"use strict";
var fs = require("fs");
var path = require("path");
var Sequelize = require("sequelize");
var basename = path.basename(module.filename);
var env = process.env.NODE_ENV || "development";
var config = require(__dirname + '/../../config/database.json')[env];
var sequelize = new Sequelize(config.database, config.username, config.password, config);
var db = {};
fs
.readdirSync(__dirname)
.filter(function(file) {
return (file.indexOf(".") !== 0) && (file !== basename);
})
.forEach(function(file) {
var model = sequelize["import"](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(function(modelName) {
if ("associate" in db[modelName]) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
In my models afterSave
hook:
afterCreate : function(user, options, fn){
mailer.sendEmailConfirmation(user.email, user.confirmation_code, function(err){
if(err){
console.log(err);
}
});
fn(null, user);
}
mailer
is a dependency required in the model. How do I go about unit testing this? I basically want to save the user in a mocha test and ensure the sendEmailConfirmation
is called.
Aucun commentaire:
Enregistrer un commentaire