Search code examples
testingmocha.jssinonstub

sinon stub is not working


I'm quiet new into testing and I don't seem to succeed to succesfully stub a function. I'm trying to stub the connection to the database, but it keep's contacting it, instead of using the result from the stub:

Here's the function:

var self = module.exports = {
VerifyAuthentication: function (data){      
    var deferred = q.defer()                
    if(typeof(data.email)=='undefined'){
        deferred.reject({data:{},errorcode:"",errormessage:"param 'email' is mandatory in input object"})
    }else{
        if(typeof(data.password)=='undefined'){
            deferred.reject({data:{},errorcode:"",errormessage:"param 'password' is mandatory in input object"})
        }else{
            var SqlString = "select id, mail, password, origin from tbl_user where mail = ?"    
            var param = [data.email]
            self.ExecuteSingleQuery(SqlString, param).then(function(results){   
                if(results.length > 0)
                {
                    if (results[0].password == data.password)
                    {                   
                        deferred.resolve({data:{"sessionId":results[0].id},errorcode:"",errormessage:""})
                    }else{              
                        deferred.reject({data:{},errorcode:"",errormessage:"bad password"})
                    }
                }else{
                    deferred.reject({data:{},errorcode:"",errormessage:"unknown user"})
                }
            })
        }
    }
    return deferred.promise
},
ExecuteSingleQuery: function (queryString, parameters){     
    var deferred = q.defer()        
    var connection = connect()

    connection.query(queryString, parameters, function (error, results, fields){
        if(error){ deferred.reject(error)};     
        deferred.resolve(results)
    });

    return deferred.promise
},

And here's the test:

var dbconnection = require('../lib/dbConnection.js') 
describe("VerifyAuthentication", function(){
    it("_Returns DbResult object when user name and password match", function(){
        var expectedResult = {data:{"sessionKey":"b12ac0a5-967e-40f3-8c4d-aac0f98328b2"},errorcode:"",errormessage:""}
        stub = sinon.stub(dbconnection, 'ExecuteSingleQuery').returns(Promise.resolve(expectedResult))
        return dbconnection.VerifyAuthentication({email:"correct@adres.com",password:"gtffr"}).then((result)=>{
            expect(result.data.sessionId).to.not.be.undefined
            expect(result.errorcode).to.not.be.undefined
            expect(result.errormessage).to.not.be.undefined
            stub.restore()                
        })
    })  
})

I always got an error 'unknown user', which is normal, because the user is indeed not in the database. However, I want to stub the 'ExecuteSingleQuery' function, avoiding it to connect to DB.


Solution

  • I have fixed a couple of issues in your code and posting the corrected files below.

    dbConnection.js

    var self = module.exports = {
        VerifyAuthentication: function (data) {
            var deferred = q.defer();
    
            if (typeof (data.email) == 'undefined') {
                deferred.reject({
                    data: {},
                    errorcode: '',
                    errormessage: "param 'email' is mandatory in input object"
                });
            } else {
                if (typeof (data.password) == 'undefined') {
                    deferred.reject({
                        data: {},
                        errorcode: '',
                        errormessage: "param 'password' is mandatory in input object"
                    });
                } else {
                    var SqlString = 'select id, mail, password, origin from tbl_user where mail = ?';
                    var param = [data.email];
    
                    self.ExecuteSingleQuery(SqlString, param).then(function (results) {
                        if (results.length > 0) {
                            if (results[0].password === data.password) {
                                deferred.resolve({
                                    data: {
                                        'sessionId': results[0].id
                                    },
                                    errorcode: '',
                                    errormessage: ''
                                });
                            } else {
                                deferred.reject({
                                    data: {},
                                    errorcode: '',
                                    errormessage: 'bad password'
                                });
                            }
                        } else {
                            deferred.reject({
                                data: {},
                                errorcode: '',
                                errormessage: 'unknown user'
                            });
                        }
                    });
                }
            }
            return deferred.promise;
        },
        ExecuteSingleQuery: function (queryString, parameters) {
            var deferred = q.defer();
            var connection = connect();
    
            connection.query(queryString, parameters, function (error, results, fields) {
                if (error) {
                    deferred.reject(error);
                }
                deferred.resolve(results);
            });
    
            return deferred.promise;
        }
    };
    

    dbConnection.test.js

    describe('VerifyAuthentication', function () {
        it('Returns DbResult object when user name and password match', function () {
            var expectedResult = [{
                id: '123',
                password: 'gtffr'
            }];
    
            const stub = sinon.stub(dbconnection, 'ExecuteSingleQuery').resolves(expectedResult);
    
            return dbconnection.VerifyAuthentication({
                email: 'correct@adres.com',
                password: 'gtffr'
            }).then((result) => {
                expect(result.data.sessionId).to.not.be.undefined;
                expect(result.errorcode).to.not.be.undefined;
                expect(result.errormessage).to.not.be.undefined;
                stub.restore();
            });
        });
    });
    

    I am outlining the problematic parts below:

    • The expectedResult variable had a wrong type of value. In the self.ExecuteSingleQuery() implementation you check for an array with length > 0. The fixed result returned by the stub, was an object instead of an array and this is why it returned the unknown user exception
    • The array should contain an object with { id: 'xxx', password: 'gtffr' } attributes. Password is validated against the one used by the dbconnection.VerifyAuthentication({email:"correct@adres.com",password:"gtffr"}) call
    • Finally, I have changed the stub statement to resolve instead of return as shown here const stub = sinon.stub(dbconnection, 'ExecuteSingleQuery').resolves(expectedResult); - this is the preferred method of resolving a promise