摩卡柴请求超时时间超过res.json

时间:2016-09-26 17:51:13

标签: json express mocha response chai

我正在使用chai代理来测试loggin,但我正在

router.get('/me', auth.isAuthenticated, function(req, res){
    res.json(new Response({error:false, results: req.session.user}))
});

在我的“/ api / v1 / account / me”中,我有:

isAuthenticated: function (req, res, next) {

    var sess = req.session;
    if(sess.user)
        return next();

    res.status(500).json(new Response({error:true})).end();
    return;
}

我的是经过验证的:

res.status(500).json(new Response({error:true})).end();

问题在于

{{1}}

永远不会返回500.如果我用状态(200)更改状态(500),一切正常(当然不是测试)。

2 个答案:

答案 0 :(得分:0)

在Describe内部或之前添加。您应该使用异步库来链接这些调用。

describe('',function(){
    this.timeout(15000);
    it('', function(done){
        Async.series([], function(cb){
            function(cb){
                cb();
            },
            function(cb){
                cb();
            }
        }, function(err){
            done();
        });
    };
});

答案 1 :(得分:0)

代码中有2个问题。

  1. func saveFlowerCategories() { let annuals = FlowerCategory(id: 1, title: "Annuals",subtitle :"Plants that just last one season. These plants will die in the winter. ", imageURL: "annuals.jpg") let perennials = FlowerCategory(id: 2, title: "Perennials",subtitle :"Plants come back year after year. These are also very less expensive", imageURL: "Perennial.jpg") let rootRef = FIRDatabase.database().reference() let categoriesRef = rootRef.child("categories") let categories = [annuals,perennials] for flowerCategory in categories { let categoryRef = categoriesRef.child("category") categoryRef.setValue(flowerCategory.toDictionary()) } } 接受两个回调参数:第一个处理成功响应(200 OK),第二个处理失败的响应(404,500等)。因此,您需要将断言代码放在第二个回调中。示例代码为:

    .then
  2. 不要使用agent.get('/api/v1/account/me') .then(function(res3) { // assertion statements when response is successful. }, function(res4) { // assertion statements when response is failed. res4.should.have.status(500); }); 。当chai断言语句失败(done)时,它将抛出res4.should.have.status(500),这意味着永远不会调用AssertionError回调 - 最终导致"错误:超时超过2000毫秒。"。相反,只需done代理调用结果就可以了:

    return