Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 即使调用计数为1,Sinon spy断言也会失败_Node.js_Unit Testing_Sinon - Fatal编程技术网

Node.js 即使调用计数为1,Sinon spy断言也会失败

Node.js 即使调用计数为1,Sinon spy断言也会失败,node.js,unit-testing,sinon,Node.js,Unit Testing,Sinon,使用Node、Sinon、Chai、proxyquire获取和Mocha 为什么要调用这个sinon-spy断言fooCallback1.should.have.been.call是否无法调用一次?我在源代码中通过console.log(fooCallback1)看到callCount为1 这是第一次也是唯一一次测试…所以我不认为有理由重置间谍 function setLight(...args) { var request; var lightNumber; var alertNa

使用Node、Sinon、Chai、proxyquire获取和Mocha

为什么要调用这个sinon-spy断言
fooCallback1.should.have.been.call是否无法调用一次?我在源代码中通过
console.log(fooCallback1)
看到
callCount
为1

这是第一次也是唯一一次测试…所以我不认为有理由重置间谍

function setLight(...args) {
  var request;
  var lightNumber;
  var alertName;
  var callback1;
  var callback2;
  var callback3;

  [request, lightNumber, alertName,
    callback1, callback2, callback3] = args;

  return fetch(request)
    .then(status)
    .then(toJSON)
    .then(() => {
      if(Boolean(callback1)) {
        console.log('one')
        callback1(lightNumber);
        console.log(callback1);
      }



以下是我编写测试的另一种方法,结果仍然相同:

  before((done)=> {
    fetch = sinon.stub().returnsPromise();
    fetchHelper = proxy('../lib/fetch-helper', {'node-fetch': fetch});
    done()
  });

  after(()=> {
  });

  it('should run fetch for light effect and shutoff', (done)=> {
    function fooCallback1() { console.log('aaaaaaaaaaaaaaaaaaaa') }
    var foo = sinon.spy(fooCallback1)
    fetchHelper.setLight('foo', 123, 'foo-alert', fooCallback1);
    var response = {
      status: 200,
      json: () => { 'foo' }
    };
    fetch.resolves(response);
    fetch.should.have.been.called;
    foo.should.have.been.called;
    done();
  });

fetchHelper.setLight()
是异步的,但是您的测试不会等待它完成,因此代码的运行顺序如下:

  • fetchHelper.setLight()
  • fetch(请求)
  • fetch.should.have.been.called
  • fooCallback1.should.have.been.called
  • done()
  • console.log('one')
  • callback1(lightNumber)
  • console.log(callback1)
您需要重写测试,以便它将等待调用回调。您没有为此使用spy,而是使用常规回调函数来测试断言:

fetchHelper.setLight()
是异步的,但是您的测试不会等待它完成,因此代码的运行顺序如下:

  • fetchHelper.setLight()
  • fetch(请求)
  • fetch.should.have.been.called
  • fooCallback1.should.have.been.called
  • done()
  • console.log('one')
  • callback1(lightNumber)
  • console.log(callback1)
您需要重写测试,以便它将等待调用回调。您没有为此使用spy,而是使用常规回调函数来测试断言:


除了robertklep的优秀答案之外,这里还有一个我让它工作的替代方法。我在调用提取包装setLight后调用提取解析:

  it('should run fetch for light effect and shutoff', (done)=> {
    var foo = sinon.spy()
    fetchHelper.setLight('foo', 123, 'foo-alert', foo); 
    fetch.resolves(response);
    fetch.should.have.been.called;
    foo.should.have.been.called;
    done();
  });

除了robertklep的优秀答案之外,这里还有一个我让它工作的替代方法。我在调用提取包装setLight后调用提取解析:

  it('should run fetch for light effect and shutoff', (done)=> {
    var foo = sinon.spy()
    fetchHelper.setLight('foo', 123, 'foo-alert', foo); 
    fetch.resolves(response);
    fetch.should.have.been.called;
    foo.should.have.been.called;
    done();
  });
  when executing setLight
one
aaaaaaaaaaaaaaaaaaaa
    1) should run fetch for light effect and shutoff
    - should set normal alert lock on
    - should set normal alert lock off after time


  0 passing (10ms)
  2 pending
  1 failing

  1) when executing setLight should run fetch for light effect and shutoff:
     AssertionError: expected fooCallback1 to have been called at least once, but it was never called
      at Context.it (test/fetch-helper.js:28:25
it('should run fetch for light effect and shutoff', done => {
  fetchHelper.setLight('foo', 123, 'foo-alert', () => {
    fetch.should.have.been.called;
    done();
  });
});
  it('should run fetch for light effect and shutoff', (done)=> {
    var foo = sinon.spy()
    fetchHelper.setLight('foo', 123, 'foo-alert', foo); 
    fetch.resolves(response);
    fetch.should.have.been.called;
    foo.should.have.been.called;
    done();
  });