在存根伪造函数上发出准备好不起作用

时间:2019-02-13 13:48:26

标签: testing ftp mocha sinon

我正在尝试测试依赖于ftp库的我的库,并且我需要控制是否调用了connect,然后触发了就绪事件,等等。

目前,我具有以下代码,并且未触发准备就绪。

const ftpClient = require('ftp');
const stubFtp = sandbox.stub(ftpClient);

  beforeEach(function(){


    doubles = {
        sftpclient: sandbox.stub(),
        rp: {request: sandbox.stub()},
        ftpclient: stubFtp 
    };

    dataFetcher = proxyquire('../../../src/myfolder/myfile',
    {
        'ssh2-sftp-client': doubles.sftpclient,
        'ftp': doubles.ftpclient,
        'request-promise': doubles.rp
    });


  });

  afterEach(function(){
    sandbox.restore();
    sinon.restore();

  });


it('should call the connect function of the ftpClient', function(){    
      let urlObj = new URL('ftp://test:test@mydomain.com/file.txt');

      const options = {
          host: urlObj.host,
          port: urlObj.port || undefined,
          user: decodeURIComponent(urlObj.username),
          password:  decodeURIComponent(urlObj.password),
          path: urlObj.pathname
      };

      sinon.stub(stubFtp.prototype, 'connect').callsFake(function(){
        this.emit('ready');
      });      

      return dataFetcher.getMetadata(new URL('ftp://test:test@mydomain.com/file.txt')).then(function(value){
        // This is never called, because there is timeout
        assert.isTrue(doubles.ftpclient.connect.calledOnce);
        assert.isTrue(doubles.ftpclient.list.calledOnce);      
      });
    });

0 个答案:

没有答案