2014-12-31 2 views
0

SuperTest의 .end() 메서드를 사용하여 응답에 Chai 어설 션을 실행하는 Sails.js API 테스트 (모카 사용)가 있습니다.SuperTest에서 어설 션 오류시 모카 타임 아웃 방지?

어설 션 후에도 테스트의 done() 콜백을 호출하지만 어설 션 오류가 발생하면 테스트 시간이 초과됩니다.

나는 시도/마침내의 주장을 포장 할 수 있지만,이 구역질 조금 보인다 : 더 나은 방법이 처리에

var expect = require('chai').expect; 
var request = require('supertest'); 

// ... 

describe('should list all tasks that the user is authorized to view', function() { 

    it('the admin user should be able to see all tasks', function (done) { 
    var agent = request.agent(sails.hooks.http.app); 
    agent 
     .post('/login') 
     .send(userFixtures.admin()) 
     .end(function (err, res) { 
     agent 
      .get('/api/tasks') 
      .expect(200) 
      .end(function (err, res) { 
      try { 
       var tasks = res.body; 
       expect(err).to.not.exist; 
       expect(tasks).to.be.an('array'); 
       expect(tasks).to.have.length.of(2); 
      } finally { 
       done(err); 
      } 
      }); 
     }); 
    }); 
}); 

어떤 제안? 아마 Chai HTTP가 좋을지도 모릅니다.

답변

1

Supertest's documentation에 따르면 err의 존재 여부를 확인하고 존재하는 경우 done 함수로 전달해야합니다. 이와 같이

.end(function (err, res) { 
    if (err) return done(err); 

    // Any other response-related assertions here 
    ... 

    // Finish the test 
    done(); 
}); 
1

테스트에서 로그인 논리를 전달할 수 있습니다.

// auth.js 
var request = require('supertest'), 
    agent = request.agent; 

exports.login = function(done) { 
    var loggedInAgent = agent(sails.hooks.http.app); 
    loggedInAgent 
     .post('/login') 
     .send(userFixtures.admin()) 
     .end(function (err, res) { 
      loggedInAgent.saveCookies(res); 
      done(loggedInAgent); 
     }); 
}; 

그리고 당신의 시험에서 사용 : 당신이 (당신이 당신의 설명 블록에 또 다시 관리자에게 다시 사용할 수 있습니다) 각 시험에 대한 귀하의 관리자 로그인 안 같은 방식으로

describe('should list all tasks that the user is authorized to view', function() { 

    var admin; 

    before(function(done) { 
     // do not forget to require this auth module 
     auth.login(function(loggedInAgent) { 
      admin = loggedInAgent; 
      done(); 
     }); 
    }); 

    it('the admin user should be able to see all tasks', function (done) { 

     admin 
      .get('/api/tasks') 
      .expect(function(res) 
       var tasks = res.body; 
       expect(tasks).to.be.an('array'); 
       expect(tasks).to.have.length.of(2); 
      }) 
      .end(done); 

    }); 

    it('should have HTTP status 200', function() { 

     admin 
      .get('/api/tasks') 
      .expect(200, done); 

    }); 

}); 

, 테스트 케이스가 더 읽기 쉽게됩니다.

.end(done)은 테스트가 오류없이 완료된다는 것을 보장하므로이 접근법에서는 시간 초과가 발생하지 않아야합니다.