简体   繁体   中英

How do I expect a function to be called with specific args with sinon, mocha, and chai in nodejs?

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.

Mike I was able to get it working finally thanks to you. I really appreciate it! Here is the final test.

describe.only('when a file is read successfully', function() {
  beforeEach(function() {
    sinon.stub(Q, 'nfcall').withArgs(fs.readFile, fileParams.path).returns(Q.resolve(file));
    sinon.stub(Q, 'ninvoke').withArgs(S3, 'upload', params).returns(Q.resolve('url'));
    chai.spy.on(Q, 'ninvoke')
  });

  it('Q.ninvoke should be called with args', function(done) {
    UploadCommand.initialize(fileParams).then(function(data) {
       expect(Q.ninvoke).to.have.been.called.with(S3, 'upload', params);
       done();
    });
  });
});

You can use sinon to stub as shown below

let yourFunctionStub
yourFunctionStub= sinon.stub(yourClass, "yourFunction");
                    yourFunctionStub.withArgs(arg1, arg2, arg3).returns(resultYouWant);

if this a promise you can return

....returns(Promise.resolve(resultYouWant));

if for argument you are not clear you can you

sinon.match.any

Hope this helps you. :)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM