2016-09-26 4 views
0

에 초과,하지만 난 내에서모카 차이 요청 시간이 다 나는 loggin에를 테스트하는 차이 에이전트를 사용하고 res.json

Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test 

it('POST /api/v1/account/me status 500', function(done) { 
    var agent = chai.request.agent(server); 
    agent.post('/api/v1/account/login') 
     .send({_email: '[email protected]', _password: 'testtest'}) 
     .then(function(res){ 
     agent.get('/api/v1/account/logout') 
      .then(function(res2){ 
       agent.get('/api/v1/account/me') 
        .then(function(res3){ 
         res2.should.have.status(500); 
         done(); 
        }); 
     }); 
    }); 
}); 

받고 있어요 "/ API/V1/계정/나", 난 이 :

router.get('/me', auth.isAuthenticated, function(req, res){ 
    res.json(new Response({error:false, results: req.session.user})) 
}); 

그리고 내 IsAuthenticated는을 :

isAuthenticated: function (req, res, next) { 

    var sess = req.session; 
    if(sess.user) 
     return next(); 

    res.status(500).json(new Response({error:true})).end(); 
    return; 
} 

문제는

,691입니다
res.status(500).json(new Response({error:true})).end(); 

은 500을 반환하지 않습니다. 상태 (200)로 상태 (500)를 변경하면 모든 것이 잘 작동합니다 (물론 테스트가 아님).

답변

0

코드에 2 가지 문제점이 있습니다.

  1. .then

    두 콜백 파라미터를 수락 : 첫번째 성공 응답 (200 OK) 번째 실패 응답 (404, 500 등)를 처리하는 처리. 따라서 두 번째 콜백 내에 어설 션 코드를 넣어야합니다. 샘플 코드는 다음과 같습니다

    agent.get('/api/v1/account/me') 
        .then(function(res3) { 
         // assertion statements when response is successful. 
        }, function(res4) { 
         // assertion statements when response is failed. 
         res4.should.have.status(500); 
        }); 
    
  2. done를 사용하지 마십시오. chai 어설 션 문이 실패하면 (res4.should.have.status(500)) AssertionError이 반환되므로 done 콜백이 호출되지 않습니다. 결국 "오류 : 2000ms 초과되었습니다."가 발생합니다. 대신에, 단지 return 에이전트가 발생할 invokation 그것은 이동하는 것이 좋다 :

    var agent = chai.request.agent(server); 
    return agent.post('/api/v1/account/login') 
          ... 
    
+0

그것은 매력처럼 작동했습니다! 정말 고맙습니다! – JVilla

0

설명 내에 또는 앞에 추가하십시오.. 그리고 비동기 라이브러리를 사용하여 이러한 호출을 연결해야합니다.

describe('',function(){ 
    this.timeout(15000); 
    it('', function(done){ 
     Async.series([], function(cb){ 
      function(cb){ 
       cb(); 
      }, 
      function(cb){ 
       cb(); 
      } 
     }, function(err){ 
      done(); 
     }); 
    }; 
}); 
+0

감사를 회신합니다. 나는 이것이 문제라고 생각하지 않는다. 또한, 내가 추가하고 싶습니다. 그() 응답을 보낼 때 실행됩니다. 그래서 그것은 비동기 함수로 잘 작동합니다. – JVilla