Javascript 带参数的mochachai单元测试
我正在使用Nodejs构建一个小库,在使用mocha和chai进行单元测试时遇到了一些问题 我的问题发生在我试图监视一个函数,并期望它被一些参数调用时 实际上,当我记录时,传递给函数的参数是好的。但测试一次又一次地失败 以下是我正在测试的内容:Javascript 带参数的mochachai单元测试,javascript,node.js,mocha.js,chai,Javascript,Node.js,Mocha.js,Chai,我正在使用Nodejs构建一个小库,在使用mocha和chai进行单元测试时遇到了一些问题 我的问题发生在我试图监视一个函数,并期望它被一些参数调用时 实际上,当我记录时,传递给函数的参数是好的。但测试一次又一次地失败 以下是我正在测试的内容: import callback from './callback' /** * Connect the express routes with the injector directly inside of the app * @param {Ob
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)
ExpressKonnector ExpressKonnector应使用pattern/users/:idUser和回调调用app.get方法:
AssertionError:预期已使用['/users/:idUser',[Function]]调用了{Spy,3次调用}
在上下文中。callback()
每次都返回一个新函数,因此它们不能相互比较
证明:
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) {
...
};
随后,您必须找到spy的函数参数,并根据预期检查其名称:
expect(spy.args[0][1].name).to.equal('callbackFunc');
其中spy.args[0][1]
表示“对spy的第一次调用([0]
)的第二个参数([1]
),它应该是由回调()生成的函数
由于spy会被调用三次,因此您可能希望迭代spy.args
,并检查每个参数是否正确。callback()
每次都返回一个新函数,因此无法相互比较
证明:
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) {
...
};
随后,您必须找到spy的函数参数,并根据预期检查其名称:
expect(spy.args[0][1].name).to.equal('callbackFunc');
其中spy.args[0][1]
表示“对spy的第一次调用([0]
)的第二个参数([1]
),它应该是由回调()生成的函数
由于spy会被调用三次,因此您可能希望迭代spy.args
,并检查每个参数是否正确