I'm new in Javascript, and I want to create units tests to test a Singleton.
So I have the following Singleton in a authentication.js file :
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;
}
};
})();
So I create a 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;
});
});
});
But when I launch 'npm test', I have the following :
1) Authentication "before all" hook:
TypeError: authenticationLib.getInstance is not a function
at Context.<anonymous> (test\authenticationTest.js:24:23)
Can anyone explain me what i'm done wrong?
Thanks a lot.
Mike.
since you did not post the whole js file i can only give you these hints.
../app/public/scripts/authentication
is the correct path edit: seems that you're missing the export
in place of returning the object, you should export it.
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.