mercredi 8 janvier 2020

Sinon how to stub method for unit testing a Async function

I am trying to write a unit test for an async function using mocha and sinon.js

Below is my test case

  describe('getOperations', function () {
    let customObj, store, someObj
    beforeEach(function () {
      someObj = {
        id: '-2462813529277062688'
      }
      store = {
        peekRecord: sandbox.stub().returns(someObj)
      }
    })
    it('should be contain obj and its ID', function () {
      const obj = getOperations(customObj, store)
      expect(obj).to.eql(someObj)
    })
  })

Below is the definition of the async function I am testing.

async function getOperations (customObj, store) {
  const obj = foo(topLevelcustomObj, store)
  return obj
}

function foo (topLevelcustomObj, store) {
    return store.peekRecord('obj', 12345)
}

The test case fails as the promise being return is rejected with a message

TypeError: store.query is not a function at Object._callee$.

The code I am testing is not calling store.query anywhere and also I have stubbed store.peekRecord also so not sure how it is getting called.




Aucun commentaire:

Enregistrer un commentaire