0

以下の作品

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

これを行う方法はありますか?

4

1 に答える 1

0

記録のために、これが私がこれを解決するためにしたことです。スーパーテストのエージェントオブジェクトとリクエストプロトタイプを変更しました。エージェントにはauthと呼ばれるメソッドが追加されました。これにより、Request.endは終了する前に最初にauthを呼び出し、次に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
于 2014-09-27T15:28:47.753 に答える