Friday 19 May 2017

Sinon spy assert fails even though call count is 1

Using Node, Sinon, Chai, proxyquire for fetch, and Mocha

How come this sinon spy assertion fooCallback1.should.have.been.called; is failing to be called once? I see with console.log(fooCallback1) in the source code that the callCount is 1.

This is the first and only test...so I don't see a reason to reset the spy.

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(()=> {
    fetch = sinon.stub().returnsPromise();

    var response = {
      status: 200,
      json: () => { 'foo' }
    };

    fetch.resolves(response);

    fetchHelper = proxy('../lib/fetch-helper', {'node-fetch': fetch});

  });

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


  1) when executing setLight should run fetch for light effect and shutoff:
     AssertionError: expected spy to have been called at least once, but it was never called
      at Context.it (test/fetch-helper.js:24:34)





  when executing setLight
    1) should run fetch for light effect and shutoff
one
{ [Function: proxy]
  isSinonProxy: true,
  formatters: 
   { c: [Function: c],
     n: [Function: n],
     D: [Function: D],
     C: [Function: C],
     t: [Function: t],
     '*': [Function: *] },
  reset: [Function: reset],
  invoke: [Function: invoke],
  named: [Function: named],
  getCall: [Function: getCall],
  getCalls: [Function: getCalls],
  calledBefore: [Function: calledBefore],
  calledAfter: [Function: calledAfter],
  calledImmediatelyBefore: [Function: calledImmediatelyBefore],
  calledImmediatelyAfter: [Function: calledImmediatelyAfter],
  withArgs: [Function: withArgs],
  matches: [Function: matches],
  printf: [Function: printf],
  calledOn: [Function],
  alwaysCalledOn: [Function],
  calledWith: [Function],
  calledWithMatch: [Function],
  alwaysCalledWith: [Function],
  alwaysCalledWithMatch: [Function],
  calledWithExactly: [Function],
  alwaysCalledWithExactly: [Function],
  neverCalledWith: [Function],
  neverCalledWithMatch: [Function],
  threw: [Function],
  alwaysThrew: [Function],
  returned: [Function],
  alwaysReturned: [Function],
  calledWithNew: [Function],
  alwaysCalledWithNew: [Function],
  callArg: [Function],
  callArgWith: [Function],
  callArgOn: [Function],
  callArgOnWith: [Function],
  yield: [Function],
  invokeCallback: [Function],
  yieldOn: [Function],
  yieldTo: [Function],
  yieldToOn: [Function],
  spyCall: { [Function: createSpyCall] toString: [Function: toString] },
  called: true,
  notCalled: false,
  calledOnce: true,
  calledTwice: false,
  calledThrice: false,
  callCount: 1,
  firstCall: 
   { proxy: [Circular],
     thisValue: undefined,
     args: [ 123 ],
     returnValue: undefined,
     exception: undefined,
     callId: 11,
     errorWithCallStack: 
      Error
          at Function.invoke (/home/one/github/lifx-weather/node_modules/sinon/lib/sinon/spy.js:205:19)
          at proxy (/home/one/github/lifx-weather/node_modules/sinon/lib/sinon/spy.js:97:22)
          at fetch.then.then.then (/home/one/github/lifx-weather/lib/fetch-helper.js:52:9)
          at process._tickCallback (internal/process/next_tick.js:103:7) },
  secondCall: null,
  thirdCall: null,
  lastCall: 
   { proxy: [Circular],
     thisValue: undefined,
     args: [ 123 ],
     returnValue: undefined,
     exception: undefined,
     callId: 11,
     errorWithCallStack: 
      Error
          at Function.invoke (/home/one/github/lifx-weather/node_modules/sinon/lib/sinon/spy.js:205:19)
          at proxy (/home/one/github/lifx-weather/node_modules/sinon/lib/sinon/spy.js:97:22)
          at fetch.then.then.then (/home/one/github/lifx-weather/lib/fetch-helper.js:52:9)
          at process._tickCallback (internal/process/next_tick.js:103:7) },
  args: [ [ 123 ] ],
  returnValues: [ undefined ],
  thisValues: [ undefined ],
  exceptions: [ undefined ],
  callIds: [ 11 ],
  errorsWithCallStack: 
   [ Error
         at Function.invoke (/home/one/github/lifx-weather/node_modules/sinon/lib/sinon/spy.js:205:19)
         at proxy (/home/one/github/lifx-weather/node_modules/sinon/lib/sinon/spy.js:97:22)
         at fetch.then.then.then (/home/one/github/lifx-weather/lib/fetch-helper.js:52:9)
         at process._tickCallback (internal/process/next_tick.js:103:7) ],
  displayName: 'spy',
  toString: [Function: toString],
  instantiateFake: [Function: create],
  id: 'spy#11' }
    - should set normal alert lock on
    - should set normal alert lock off after time


  0 passing (15ms)
  2 pending
  1 failing

  1) when executing setLight should run fetch for light effect and shutoff:
     AssertionError: expected spy to have been called at least once, but it was never called
      at Context.it (test/fetch-helper.js:27:34)



via dman

No comments:

Post a Comment