【问题标题】:How to test function returning next method with Hippie?如何使用 Hippie 测试返回下一个方法的函数?
【发布时间】:2015-09-10 20:51:10
【问题描述】:

我有一个返回 next(); 的函数作为允许调用下一个路由的函数,它用于检查令牌作为拦截器并允许函数通过。

我现在遇到的问题是我想测试这个特定的功能,但我没有下一条路线,所以当我使用一些 API 测试工具(如 Hippie)并发出请求时,它挂起并且什么都不做,直到定时出去。

所以我的测试电话如下Hippie

    hippie(server)
        .header('token', token)
        .json()
        .put('/token')
        .send(tokenRequest)
        .end(function(err, res, body) {
            if (err) {
                throw err;
            }

            done();
        });

基本上令牌端点链接到令牌拦截器并返回 next();

如何使它进入可测试状态?

【问题讨论】:

    标签: javascript node.js restify


    【解决方案1】:

    函数不应该返回 next(),它应该在完成操作后调用 next。例如:

        module.router.post('/*', function(req,res,next){
           middleware.doSomething()
           .then(function(){
              next();
           });
        });
    
        module.router.post('/user', function(req,res,next){
           //comes here from the next() above
           res.status(501).send("unimplemented");
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-07
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      • 2015-03-01
      • 2021-06-23
      相关资源
      最近更新 更多