Reputation: 4946
I have a function that I need to test using sinon. It takes two arguments and has different events that can be raised. I'm trying to simulate the 'ready' evet made to simulate a successful SFTP connection
function configureSFTPConnection(conn, connectionSettings) {
'use strict';
return new Promise(function(resolve, reject) {
conn.on('ready', function() {
resolve(conn);
}).on('error', function(err) {
reject(err);
}).connect(connectionSettings);
});
}
I can simulate the exterior connect
function.
configureSftpStub = sinon.stub(clientObject, 'connect');
How can I force the ready
callback to execute, completing the promise?
This is what I'm trying:
clientObject = new client();
configureSftpStub = sinon.stub(clientObject, 'connect');
configureSftpStub.onCall(0).returns(function() {
console.log('trying to do something');
resolve();
});
The .onCall()
never seems to run.
Upvotes: 0
Views: 1355
Reputation: 4946
What was needed was rather than trying to return something I needed to replace the function that was called and do a simple .emit
call within the function.
configureSftpStub = sinon.stub(clientObject, 'connect', function() {
this.emit('ready');
});
Upvotes: 1