首页 文章

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

提问于
浏览
2

我一直有一个问题,试图确保用我传入的args调用 Q.ninvoke . 我是新手测试Sinon,Mocha和Chai . 我已经尝试了我在网上找到的所有东西,现在已经有两天了,我仍然无法获得我的测试通行证 . 我究竟做错了什么?

这是我的测试代码 .

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 Q.ninvoke应该使用args调用:AssertionError:使用[Array(3)]调用期望

3 回答

  • 4

    试试这个:

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

    请特别注意,您应该从初始化函数返回一个promise . 然后在测试中:

    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”函数,然后在函数体中,函数就是应用程序代码的其余部分 . 另外,'完成'回调是告诉mocha它是异步测试的方式 .

  • 0

    迈克,我终于得到了它,感谢你 . 对此,我真的非常感激!这是最后的测试 .

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

    您可以使用sinon来存根,如下所示

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

    如果这是一个你可以回来的承诺

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

    如果你的论点不清楚,你可以

    sinon.match.any
    

    希望这对你有所帮助 . :)

相关问题