0

I'm trying to write some tests where I need to authenticate first. If I make multiple requests in "before()" I get connection refused. If I split it between "before()" and "it()" it works but I cannot acheive what I want.

Code I want to work:

var agent = request.agent(myExpressApp),
      token;
    before(function(done) {
      async.series([
        function(cb) {
          agent
            .post('/1/auth/login')
            .send({
              email: 'john@smith.com',
              password: 'j0hn_Sm1TH'
            })
            .expect(200)
            .end(cb);
        }, function(cb) {
          agent
            .get('/1/auth/authorize')
            .query({
              response_type: 'token',
              client_id: 'some id',
              redirect_uri: 'http://ignore'
            })
            .expect(302)
            .end(function(err, res) {
              /* console.log(arguments) = { '0': 
                { [Error: connect ECONNREFUSED]
                  code: 'ECONNREFUSED',
                  errno: 'ECONNREFUSED',
                  syscall: 'connect' } }*/
              if (err) return cb(err);
              cb();
            });
        }
      ], done);
    });
    it('some authenticated task', function(done) {
      // do something else
      done();
    });

Code that is working:

var agent = request.agent(myExpressApp),
      token;
    before(function(done) {
      async.series([
        function(cb) {
          agent
            .post('/1/auth/login')
            .send({
              email: 'john@smith.com',
              password: 'j0hn_Sm1TH'
            })
            .expect(200)
            .end(cb);
        }, function(cb) {
          cb();
        }
      ], done);
    });
    it('some authenticated task', function(done) {
      agent
        .get('/1/auth/authorize')
        .query({
          response_type: 'token',
          client_id: 'some id',
          redirect_uri: 'http://ignore'
        })
        .expect(302)
        .end(function(err, res) {
          if (err) return done(err);
          done();
        });
    });
4

1 回答 1

0

您遇到了 superagent 的问题 153。令人讨厌和神奇的是,superagent 会查看您传递给它的回调函数的数量。如果函数声明接受 2 个参数,则 superagent 符合 的节点约定(error, result),但是,如果回调函数看起来需要任何其他数量的参数(在与 async.js 一起使用的情况下为零),它会调用函数为我猜callback(res)TJ 认为这对浏览器来说会很好,但对于节点来说,它完全违反了惯例。

有问题的确切代码行在这里

于 2014-06-01T16:02:05.470 回答