CodeFoodPixels / node-promise-mysql

A wrapper for mysqljs/mysql that wraps function calls with Bluebird promises.
MIT License
338 stars 64 forks source link

Mock data using sinon for Unit Testing #42

Closed shriharry closed 7 years ago

shriharry commented 7 years ago

Hi ,

Can we mock data using sinon for this package. As Sinon allows us to mock and / or stub out methods on objects. Say I want to override the createConnection() method so that it returns a valid object regardless of whether the database settings were right or not, I'd stub out that method. I am able to mock that using 'mysql' package which is having callbacks.

But, as this package returns promises, I am not sure about how to mock test data for same.

For 'mysql' package this working perfectly.

var successConnectionObject = {
        connect: function(cb) {
            cb();
        },
        query: function(sqlQury, cb) {
            if (sqlQury === 'SELECT * FROM USERS') {
                cb(null, constants.USERQueryResp);
            } else {
                cb(null, '');
            }
        },
        end: function() {} 
}

var testMysqlDriver = require('mysql');
var stub = sinon.stub(testMysqlDriver, 'createConnection');
stub.returns(successConnectionObject);

 publish.handler(some_JSON_Request, some_Context_Values, function(err, data) {
    expect(data.message).toBe(USER_Query_Response);
    done();
}); 

how can we achieve same this using require('promise-mysql');

CodeFoodPixels commented 7 years ago

Hey, just wanted to let you know that I've seen this and I'm not ignoring it. I have an idea of what to do and need to investigate a little before suggesting a solution. Hopefully I get some time tonight or tomorrow.

On Mon, 6 Feb 2017, 14:43 Shrikant Haralayya, notifications@github.com wrote:

Hi ,

Can we mock data using sinon for this package. As Sinon allows us to mock and / or stub out methods on objects. Say I want to override the createConnection() method so that it returns a valid object regardless of whether the database settings were right or not, I'd stub out that method. I am able to mock that using 'mysql' package which is having callbacks.

But, as this package returns promises, I am not sure about how to mock test data for same.

For 'mysql' package this working perfectly.

var successConnectionObject = { connect: function(cb) { cb(); }, query: function(sqlQury, cb) { if (sqlQury === 'SELECT * FROM USERS') { cb(null, constants.USERQueryResp); } else { cb(null, ''); } }, end: function() {} }

var testMysqlDriver = require('mysql'); var stub = sinon.stub(testMysqlDriver, 'createConnection'); stub.returns(successConnectionObject);

publish.handler(some_JSON_Request, some_Context_Values, function(err, data) { expect(data.message).toBe(USER_Query_Response); done(); });

how can we achieve same this using require('promise-mysql');

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/lukeb-uk/node-promise-mysql/issues/42, or mute the thread https://github.com/notifications/unsubscribe-auth/AEjy1DMJ58RYP8QOBoP9i7TkBt-Bkk3Xks5rZzGVgaJpZM4L4RX3 .

CodeFoodPixels commented 7 years ago

So you'll need to include bluebird into your test file. Then want to wrap your successConnectionObject with a bluebird.resolve(). Next you'll want to make sure that instead of calling cb() that you return a promise.

I couldn't do much with your code as it seems to rely on other things that aren't included, so I can't give a solid example.