I m building a little library using Nodejs and I m having some troubles unit testing with mocha and chai.
My problem happens when I try to spy on a function, and expect it to have been called with some parametes.
Actually, when I log, the parameters passed to the function are good. But the test is failing again and again.
Here's what I m testing :
import callback from './callback'
/**
* Connect the express routes with the injector directly inside of the app
* @param {Object} app An express application, or an application that has the same api
* @param {Object} injector An injector with the interface of deepin module
* @param {Object} parser An object with the interface of route-parser module
*/
const expressKonnector = (app, injector, parser) => {
const routes = parser.parseRoutes()
for (const route of routes) {
app[route.method](route.pattern, callback(injector, route))
}
}
export default expressKonnector
Here's the callback dependent module :
import callbackRender from './callbackRender'
import { HttpRequest } from 'default-http'
export default (injector, route) => {
const ctrl = injector.get(route.controller)
return (request, response) => {
const result = ctrl[route.controllerMethod](new HttpRequest())
if (result.then) {
return result.then(res => callbackRender(res, response))
} else {
callbackRender(result, response)
}
}
}
And here's the failing test :
it('should call the app.get method with pattern /users/:idUser and a callback', () => {
const spy = chai.spy.on(app, 'get')
const routes = routeParser.parseRoutes()
expressKonnector(app, injector, routeParser)
expect(spy).to.have.been.called.with('/users/:idUser', callback(injector, routes[1]))
})
I have the following stack when the test fails :
ExpressKonnector ExpressKonnector should call the app.get method with pattern /users/:idUser and a callback:
AssertionError: expected { Spy, 3 calls } to have been called with [ '/users/:idUser', [Function] ]
at Context.<anonymous> (C:/Project/javascript/express-konnector/src/test/expressKonnector.spec.js:176:43)
If you want to have more detail, or simply run the "npm install && npm test command", you can have the module on this github (dev branch) :