简体   繁体   English

带有参数的单元测试Mocha Chai

[英]Unit testing mocha chai with arguments

I m building a little library using Nodejs and I m having some troubles unit testing with mocha and chai. 我正在使用Nodejs构建一个小的库,并且在使用mocha和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) : 如果您想了解更多细节,或者只是运行“ npm install && npm test命令”,则可以在此github(dev分支)上安装该模块:

https://github.com/Skahrz/express-konnector https://github.com/Skahrz/express-konnector

callback() returns a new function every time, so they can't be compared to each other. callback()每次都会返回一个新函数,因此无法将它们相互比较。

To demonstrate: 展示:

const giveFunc = () => () => 'bar';

let func1 = giveFunc();
let func2 = giveFunc();
console.log( func1 === func2 ); // false

You can do a partial match instead, to validate the first argument: 您可以执行部分​​匹配来验证第一个参数:

expect(spy).to.have.been.called.with('/users/:idUser');

If you really want to test if the right function gets passed, you can't use an anonymous function, so you'll have to name it: 如果您真的想测试是否传递了正确的函数,则不能使用匿名函数,因此必须命名它:

return function callbackFunc(request, response) {
  ...
};

You subsequently have to find the function argument to the spy, and check its name against what's expected: 随后,您必须找到间谍的function参数,并根据预期检查其名称:

expect(spy.args[0][1].name).to.equal('callbackFunc');

Where spy.args[0][1] means "the second argument ( [1] ) for the first call ( [0] ) to the spy" , which should be the function generated by callback() . 其中spy.args[0][1]表示“对间谍的第一次调用( [0] )的第二个参数( [1] )” ,它应该是callback()生成的函数。

Since your spy gets called three times, you probably want to iterate over spy.args and check each one for the correct function argument. 由于您的间谍被调用了三次,因此您可能想要遍历spy.args并检查每个参数是否有正确的函数参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM