请求挂起帆错误

时间:2014-07-12 00:46:38

标签: mocha sails.js supertest superagent

如果您通过supertest向sails发出请求,则在您返回错误时响应会挂起。 在这里,我们已经解除了风帆,并将其作为针对实时数据库的集成测试运行。

var sails = require('sails').lift();
var request = require('supertest');
var app = sails.hooks.http.app;
describe('The creation of a model',function(){
    it('should not create a duplicate',function(done){

        var user = request.agent(app);
        user
            .post('/api/create')
            .end(function(err,res){

                //never gets here, your test will hang
                done();

            });

    });
});


//controller.js

module.exports = {
    // /api/create routes here
    create:function(req,res){

        var params = {
            name:"invalid"
        };
        SomeAction(params,function(err,results){

            if (err) {
                //the problem is here.
                return err;
            }
            res.json(results);

        });

    }

};

1 个答案:

答案 0 :(得分:0)

如果你对帆进行了超级请求,那么只需就会返回一个值,即。你没有使用res.send()或res.json()它会挂起supertest的请求。这是正确的方法:

var sails = require('sails').lift();
var request = require('supertest');
var app = sails.hooks.http.app;
describe('The creation of a model',function(){
    it('should not create a duplicate',function(done){

        var user = request.agent(app);
        user
            .post('/api/create')
            .end(function(err,res){

                //never gets here, your test will hang
                done();

            });

    });
});


//controller.js

module.exports = {
    // /api/create routes here
    create:function(req,res){

        var params = {
            name:"invalid"
        };
        SomeAction(params,function(err,results){

            if (err) {
                //you need to send a response to the 
                res.json({
                  error:"an error occured"
                });
            }
            res.json(results);

        });

    }

};