我如何期望在 nodejs 中使用 sinon、mocha 和 chai 的特定参数调用函数?

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

我在尝试确保使用我传递的参数调用 Q.ninvoke 时遇到了问题。我是 Sinon、Mocha 和 Chai 的新手。我已经尝试了 2 天在网上找到的所有内容,但我仍然无法通过考试。我做错了什么?

这是我正在测试的代码。

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;
    });
  });
}

这是我的测试。

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);
    });
});

这是我遇到的错误。

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) ]

试试这个:

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);
  });
}

请特别注意,您应该 return 来自初始化函数的承诺。然后在测试中:

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();
    });
  });
});

还有一些其他需要注意的事情,在您的主应用程序代码中,您还需要将初始化函数链接到一个 'then' 函数,然后函数的主体是其余的你的应用程序代码应该去。此外,'done' 回调是您告诉 mocha 这是一个异步测试的方式。

Mike 多亏了你,我终于让它工作了。我真的很感激!这是最后的测试。

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();
    });
  });
});

你可以使用sinon来存根,如下图

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

如果这是一个承诺,你可以 return

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

如果对于争论你不清楚你可以

sinon.match.any

希望对您有所帮助。 :)