【问题标题】:agent.auth before each test in a describe block在描述块中的每个测试之前的 agent.auth
【发布时间】:2014-11-14 16:43:23
【问题描述】:

以下有效

describe('My App', function() {
  describe('when logged in', function() {
    it('should allow registered user to make a thing', function(done) {
      agent.post('/make-a-thing')
      .auth('testusername', 'validuserpass')
      .send({thingName:'mythingname'})
      .expect(201)
      .end(function(err, res) {
        if (err) return done(err);
        res.body.should.have.property('thingUrl').and.to.match(/thing\/[0-9a-f]+$/);
        done();
      });
    });
  });
});

现在,如果我想在“登录时”块中添加越来越多的测试,我不想每次都重复 .auth('testusername', 'validuserpass') 行。我应该把认证代码放在 beforeEach 中,因为这就是 beforeEach 的用途。

所以我尝试了这个:

describe("My App", function() {

  describe('when logged out', function() {
    it('should disallow anonymous user from doing things', function(done) {
      agent.post('/do-things')
      .send({thingName:'mythingname'})
      .expect(403)
      .end(function(err, res) {
        if (err) return done(err);
        done();
      });
    });
  });

  describe('when invalid user', function() {
    beforeEach(function(done) {
      agent.auth('invalidusername', 'invaliduserpass');
      done();
    });

    it('should disallow unrecognized user from doing things', function(done) {
      agent.post('/do-things')
      .send({thingName:'mythingname'})
      .expect(403)
      .end(function(err, res) {
        if (err) return done(err);
        done();
      });
    });
  })

  describe('when logged in', function() {
    beforeEach(function(done) {
      agent.auth('testusername', 'validuserpass');
      done();
    });

    it('should allow registered user to make a thing', function(done) {
      agent.post('/make-a-thing')
      .send({thingName:'mythingname'})
      .expect(201)
      .end(function(err, res) {
        if (err) return done(err);
        res.body.should.have.property('thingUrl').and.to.match(/thing\/[0-9a-f]+$/);
        done();
      });
    });

    it('should require name attribute to create a thing', function(done) {
      agent.post('/make-a-thing')
      .send({notaname:'notathingname'})
      .expect(409)
      .expect('Content-Type', /json/)
      .end(function(err, res) {
        if (err) return done(err);
        done();
      });
    });
  });

});

发生的情况是 agent.auth 未定义。我认为auth 方法是在auth.post 的结果中定义的。

有没有办法做到这一点?

【问题讨论】:

    标签: node.js mocha.js supertest superagent


    【解决方案1】:

    为了记录,这是我为解决这个问题所做的。我修改了 supertest 的 agent 对象和 Request 原型。 agent 现在有一个名为 auth 的方法,它使 Request.end 在结束之前首先调用 auth,然后恢复 em>Request.end 回到原来的状态。

    configure.js

    var app = require('app'),
        supertest = require('supertest');
    
    // global
    agent = supertest.agent(app);
    
    (function(Request) {
      'use strict';
    
      (function(_end) {
        agent.auth = function() {
          var authArgs = arguments;
          Request.end = function() {
            var endArgs = arguments;
            var endResult = _end.apply(this.auth.apply(this, authArgs), endArgs);
            Request.end = _end;
            return endResult;
          };
          return agent;
        };
      })(Request.end);
    })(agent.post('').constructor.prototype);
    

    app-test.js

    describe("My App", function() {
    
      describe('when logged out', function() {
        it('should disallow anonymous user from doing things', function(done) {
          agent.post('/do-things')
          .send({thingName:'mythingname'})
          .expect(403)
          .end(function(err, res) {
            if (err) return done(err);
            done();
          });
        });
      });
    
      describe('when invalid user', function() {
        beforeEach(function(done) {
          agent.auth('invalidusername', 'invaliduserpass');
          done();
        });
    
        it('should disallow unrecognized user from doing things', function(done) {
          agent.post('/do-things')
          .send({thingName:'mythingname'})
          .expect(403)
          .end(function(err, res) {
            if (err) return done(err);
            done();
          });
        });
      })
    
      describe('when logged in', function() {
        beforeEach(function(done) {
          agent.auth('testusername', 'validuserpass');
          done();
        });
    
        it('should allow registered user to make a thing', function(done) {
          agent.post('/make-a-thing')
          .send({thingName:'mythingname'})
          .expect(201)
          .end(function(err, res) {
            if (err) return done(err);
            res.body.should.have.property('thingUrl').and.to.match(/thing\/[0-9a-f]+$/);
            done();
          });
        });
    
        it('should require name attribute to create a thing', function(done) {
          agent.post('/make-a-thing')
          .send({notaname:'notathingname'})
          .expect(409)
          .expect('Content-Type', /json/)
          .end(function(err, res) {
            if (err) return done(err);
            done();
          });
        });
      });
    
    });
    

    我的测试是这样运行的,所以先执行configure脚本:

    mocha tests/configure.js tests/*-test.js
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多