Node.js 如何使用sinon从节点流存根错误?

Node.js 如何使用sinon从节点流存根错误?,node.js,gulp,mocha.js,sinon,child-process,Node.js,Gulp,Mocha.js,Sinon,Child Process,我正在测试一个使用的gulp插件,我正在尝试监视spawn,以便返回一个错误。对于sinon,我尝试了使用.returns和.throws,但没有成功 index.js -------- const cp = require('child_process'); const PluginError = require('plugin-error'); const through = require('through2'); module.exports = () => { return

我正在测试一个使用的gulp插件,我正在尝试监视spawn,以便返回一个错误。对于sinon,我尝试了使用.returns和.throws,但没有成功

index.js
--------
const cp = require('child_process');
const PluginError = require('plugin-error');
const through = require('through2');

module.exports = () => {
  return through.obj(function (file, enc, cb) {
    const convert = cp.spawn('convert');

    convert.on('error', (err) => {
      cb(new PluginError(PLUGIN_NAME, 'ImageMagick not installed'));
      return;
    });
  });
}

test.js
-------
const cp = require('child_process');
const expect = require('chai').expect;
const plugin = require('../index');
const sinon = require('sinon');
const Vinyl = require('vinyl');

it('error when ImageMagick not installed', (done) => {
  sinon.stub(cp, 'spawn'); // Problem area: I tried .returns and .throws
  const vinyl = new Vinyl();

  const stream = plugin();
  stream.write(vinyl);
  stream.once('error', (error) => {
    expect(error);
    done();
  });
});

我忘了存根生成返回的子进程

let mockProcess = {
  on: sinon.stub(),
  stdout: {
    on: sinon.stub()
  },
  stderr: {
    on: sinon.stub()
  }
};
mockProcess.on.withArgs('error').yieldsAsync(new Error());
sinon.stub(cp, 'spawn').returns(mockProcess);
sinon.stub(cp,'spawn')。返回(“hello”)适合我。也许你可以在你的代码中展示你是如何使用它的。在加载使用
spawn