I have been having a problem trying to make sure Q.ninvoke
is called with the args I am passing in. I am new to testing with Sinon, Mocha and Chai. I have been trying everything I have found online for 2 days now and I still cant get my test pass. What am I doing wrong?
This is my code under test.
var cuid = require('cuid');
var fs = require('fs');
var Q = require('q');
var AWS = require('aws-sdk');
var S3 = new AWS.S3();
module.exports = {
initialize: initialize
};
function initialize(options) {
return Q.nfcall(fs.readFile, options.path).then(function (file) {
var fileParams = {
Bucket: options.bucket,
Key: options.name,
Body: file,
ContentType: options.contentType
};
return Q.ninvoke(S3, 'upload', fileParams).then(function(data){
return data.Location;
});
});
}
Here is my test.
describe.only('when a file is read successfully', function() {
var spy;
beforeEach(function() {
spy = chai.spy.on(Q, 'ninvoke');
sinon.stub(Q, 'nfcall').withArgs(fs.readFile, fileParams.path).returns(Q.resolve(file));
});
it('Q.ninvoke should be called with args', function() {
UploadCommand.initialize(fileParams)
expect(spy).to.have.been.called.with(S3, 'upload', params);
});
});
This is the error I am getting.
1) UploadCommand .initialize when a file is read successfully Q.ninvoke should be called with args: AssertionError: expected { Spy } to have been called with [ Array(3) ]
try this:
var cuid = require('cuid');
var fs = require('fs');
var Q = require('q');
var AWS = require('aws-sdk');
var S3 = new AWS.S3();
module.exports = {
initialize: initialize
};
function initialize(options) {
return Q.nfcall(fs.readFile, options.path).then(function (file) {
var fileParams = {
Bucket: options.bucket,
Key: options.name,
Body: file,
ContentType: options.contentType
};
return Q.ninvoke(S3, 'upload', fileParams);
});
}
note in particular that you should return a promise from your initialize function. then in the test:
describe.only('when a file is read successfully', function() {
var spy;
beforeEach(function() {
spy = chai.spy.on(Q, 'ninvoke');
sinon.stub(Q, 'nfcall').withArgs(fs.readFile,fileParams.path).returns(Q.resolve(file));
});
it('Q.ninvoke should be called with args', function(done) {
UploadCommand.initialize(fileParams).then(function(data) {
expect(spy).to.have.been.called.with(S3, 'upload', params);
done();
});
});
});
a couple of other things to note, in your main application code, you will also want to chain your initialize function to a 'then' function, and in the body of that then function is where the rest of your application code should go. also, the 'done' callback is the way you tell mocha that it is an asynchronous test.