[英]How to test with Mocha a Javascript Singleton
我是Javascript的新手,我想創建單元測試來測試Singleton。
所以我在authentication.js文件中有以下Singleton:
var AuthenticationService = (function () {
/**
* Instance du singleton
*/
var instance;
/**
* Private property du service d'authentification de firebase.
* @type {firebase.auth.Auth}
*/
var privateFirebaseAuthService = null;
function init(firebaseAuthService) {
privateFirebaseAuthService = firebaseAuthService;
/**
* Crée un nouvel utilisateur de l'application
* @param {String} email l'email de l'utilisateur
* @param {String} password le password de l'utilisateur
* @return {Promise<firebase.User>} Renvoie l'utilisateur créé en cas de succès.
*/
function privateCreateUserAsync(email, password) {
return new Promise(function(resolve, reject){
if(privateFirebaseAuthService === null){
console.log("Le service d'authentification firebase n'est pas initialisé.");
reject(Error(Enum.Authentication.CreateUserErrorCode.AuthenticationServiceNotInitialized));
}
privateFirebaseAuthService.createUserWithEmailAndPassword(email, password)
.then(function(firebaseUser) {
console.log("createUserAsync ok " + email);
// TODO : renvoyer un application user plutôt qu'un user firebase.
resolve(firebaseUser);
})
.catch(function(error) {
var errorCode = error.code;
console.log("createUserAsync KO " + errorCode);
if (errorCode == 'auth/email-already-in-use') {
reject(Error(Enum.Authentication.CreateUserErrorCode.EmailAlreadyUsed));
}
else if(errorCode == 'auth/invalid-email'){
reject(Error(Enum.Authentication.CreateUserErrorCode.InvalidEmail));
}
else if(errorCode == 'auth/operation-not-allowed'){
reject(Error(Enum.Authentication.CreateUserErrorCode.OperationNotAllowed));
}
else if(errorCode == 'auth/weak-password'){
reject(Error(Enum.Authentication.CreateUserErrorCode.WeakPassword));
}
else{
reject(Error(Enum.Authentication.CreateUserErrorCode.Unknown));
}
});
});
}
return {
// Public methods and variables
createUserAsync: function(email, password){
return privateCreateUserAsync(email, password);
}
};
};
return {
// Get the Singleton instance if one exists
// or create one if it doesn't
getInstance: function(firebaseAuthService) {
if ( !instance ) {
instance = init(firebaseAuthService);
}
return instance;
}
};
})();
所以我創建了一個authenticationTest.js:
var chai = require('chai');
var expect = chai.expect;
var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
var firebase = require('firebase');
var authenticationLib = require('../app/public/scripts/authentication');
describe("Authentication", function(){
before(function(){
// dev firebase 3
var config = {
apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
authDomain: "xxxxxxxxxxxxxxxxxxxxxxxxx",
databaseURL: "xxxxxxxxxxxxxxxxxxxxxx",
storageBucket: "xxxxxxxxxxxxxxxxxxxxxxxx",
};
firebase.initializeApp(config);
var firebaseAuthService = firebase.auth();
console.log(authenticationLib);
authenticationLib.getInstance(firebaseAuthService);
});
describe("Create User", function(){
it("should be return a rejected promise with EmailAlreadyUsed error", function(){
authenticationLib.getInstance().createUserAsync('hfdzjfezzpf@fezkfjezofez.fr', 'dhkofefzefs456fefz45').should.be.fulfilled;
});
});
});
但是當我啟動'npm test'時,我有以下內容:
1) Authentication "before all" hook:
TypeError: authenticationLib.getInstance is not a function
at Context.<anonymous> (test\authenticationTest.js:24:23)
誰能解釋一下我做錯了什么?
非常感謝。
麥克風。
因為你沒有發布整個js文件我只能給你這些提示。
../app/public/scripts/authentication
是否是正確的路徑 編輯:似乎你錯過了導出
代替返回對象,您應該導出它。
module.export = {
// Get the Singleton instance if one exists
// or create one if it doesn't
getInstance: function(firebaseAuthService) {
if ( !instance ) {
instance = init(firebaseAuthService);
}
return instance;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.