title: How do I expect a function to be called with specific args with sinon, mocha, and chai in nodejs?
body: <p>I have been having a problem trying to make sure <code>Q.ninvoke</code> 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?</p>
  
  <p>This is my code under test.</p>
  
  <pre><code>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;
      });
    });
  }
  </code></pre>
  
  <p>Here is my test.</p>
  
  <pre><code>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);
      });
  });
  </code></pre>
  
  <p>This is the error I am getting.</p>
  
  <blockquote>
    <p>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) ]</p>
  </blockquote>