带参数的单元测试 mocha chai
Unit testing mocha chai with arguments
我正在使用 Nodejs 构建一个小库,但在使用 mocha 和 chai 进行单元测试时遇到了一些麻烦。
我的问题发生在我试图监视一个函数,并期望它被调用时带有一些参数。
其实我在登录的时候,传递给函数的参数是好的。但是测试一次又一次失败。
这是我正在测试的内容:
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
这里是回调依赖模块:
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)
}
}
}
这是失败的测试:
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]))
})
测试失败时我有以下堆栈:
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)
如果您想了解更多详细信息,或者只是 运行 "npm install && npm test command",您可以在此 github(开发分支)上获取模块:
callback()
returns每次都是一个新的函数,不能相互比较
演示:
const giveFunc = () => () => 'bar';
let func1 = giveFunc();
let func2 = giveFunc();
console.log( func1 === func2 ); // false
您可以改为进行部分匹配,以验证第一个参数:
expect(spy).to.have.been.called.with('/users/:idUser');
如果你真的想测试是否传递了正确的函数,你不能使用匿名函数,所以你必须给它命名:
return function callbackFunc(request, response) {
...
};
您随后必须找到间谍的函数参数,并根据预期检查其名称:
expect(spy.args[0][1].name).to.equal('callbackFunc');
其中spy.args[0][1]
表示"the second argument ([1]
) for the first call ([0]
) to the spy",应该是callback()
.
生成的函数
因为你的间谍被调用了三次,你可能想要遍历 spy.args
并检查每个函数参数是否正确。
我正在使用 Nodejs 构建一个小库,但在使用 mocha 和 chai 进行单元测试时遇到了一些麻烦。
我的问题发生在我试图监视一个函数,并期望它被调用时带有一些参数。
其实我在登录的时候,传递给函数的参数是好的。但是测试一次又一次失败。
这是我正在测试的内容:
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
这里是回调依赖模块:
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)
}
}
}
这是失败的测试:
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]))
})
测试失败时我有以下堆栈:
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)
如果您想了解更多详细信息,或者只是 运行 "npm install && npm test command",您可以在此 github(开发分支)上获取模块:
callback()
returns每次都是一个新的函数,不能相互比较
演示:
const giveFunc = () => () => 'bar';
let func1 = giveFunc();
let func2 = giveFunc();
console.log( func1 === func2 ); // false
您可以改为进行部分匹配,以验证第一个参数:
expect(spy).to.have.been.called.with('/users/:idUser');
如果你真的想测试是否传递了正确的函数,你不能使用匿名函数,所以你必须给它命名:
return function callbackFunc(request, response) {
...
};
您随后必须找到间谍的函数参数,并根据预期检查其名称:
expect(spy.args[0][1].name).to.equal('callbackFunc');
其中spy.args[0][1]
表示"the second argument ([1]
) for the first call ([0]
) to the spy",应该是callback()
.
因为你的间谍被调用了三次,你可能想要遍历 spy.args
并检查每个函数参数是否正确。