lundi 6 juin 2016

Chai and spies doesn't get call with good args

I m testing an application in which I need to test some callbacks behaviour.

I've isolated the callback and tried to make something testable.

I m having these two functions :

const callbackRender = (httpResponse, response) => {
  if (httpResponse.content.content) response.send(httpResponse.content.content)
  else response.render(httpResponse.content.page)
}

const callback = (injector, route) => {
  return (request, response) => {
    const ctrl = injector.get(route.controller)
    const httpResponse = ctrl[route.controllerMethod](new HttpRequest())
    callbackRender(httpResponse, response)
  }
}

The test that I have failing is the following :

  it('should call the callback render method when httpResponse is not a promise', () => {
      const mock = sinon.mock(injector)
      const ctrl = new UserControllerMock()
      const routes = routeParser.parseRoutes()
      mock.expects('get').returns(ctrl)
      const spy = chai.spy.on(callbackRender)
      callback(injector, routes[1])(request, response)
      const controllerResult = ctrl.getAll(new HttpRequest())
      expect(spy).to.have.been.called.with(controllerResult, response)
      mock.verify()
      mock.restore()
    })

The problem happens on the spy that has not been called.

But when I log the expected result, and if I log what I pass as parameter in the callbackRender, I have exactly the same JSON object.

Here's the chai :

AssertionError: expected { Spy } to have been called with [ Array(2) ]

Aucun commentaire:

Enregistrer un commentaire