2014-01-24 2 views
9

통합 테스트를 작성하는 다음 경로 (명시 적)가 있습니다.Sinon-Chai를 사용할 때 "오류 : 2000ms 초과"오류 메시지가 표시됩니다.

여기에 코드입니다 :

var q = require("q"), 
    request = require("request"); 

/* 
    Example of service wrapper that makes HTTP request. 
*/ 
function getProducts() { 

    var deferred = q.defer(); 

    request.get({uri : "http://localhost/some-service" }, function (e, r, body) { 
     deferred.resolve(JSON.parse(body)); 
    }); 

    return deferred.promise; 
} 

/* 
    The route 
*/ 
exports.getProducts = function (request, response) { 
    getProducts() 
     .then(function (data) { 
      response.write(JSON.stringify(data)); 
      response.end(); 
     }); 
}; 

내가 모든 구성 요소가 함께하지만 가짜 HTTP 응답으로 작동하는지 테스트 할, 그래서 요청/HTTP 상호 작용의 그루터기를 만드는 오전.

나는 Chai, Sinon 및 Sinon-Chai와 Mocha를 테스트 러너로 사용하고 있습니다. 응답 (Response.Write를)에 기록 된 인수가 테스트가 확인 통과 일치하는 경우

var chai = require("chai"), 
    should = chai.should(), 
    sinon = require("sinon"), 
    sinonChai = require("sinon-chai"), 
    route = require("../routes"), 
    request = require("request"); 

chai.use(sinonChai); 

describe("product service", function() { 
    before(function(done){ 
     sinon 
     .stub(request, "get") 
     // change the text of product name to cause test failure. 
     .yields(null, null, JSON.stringify({ products: [{ name : "product name" }] })); 
     done(); 
    }); 

    after(function(done){ 
     request.get.restore(); 
     done(); 
    }); 

    it("should call product route and return expected resonse", function (done) { 

     var writeSpy = {}, 
      response = { 
      write : function() { 
       writeSpy.should.have.been.calledWith("{\"products\":[{\"name\":\"product name\"}]}"); 
       done(); 
      } 
     }; 

     writeSpy = sinon.spy(response, "write"); 

     route.getProducts(null, response); 
    }); 
}); 

:

다음은 테스트 코드입니다. 그러나 그것은 문제가 해결되지 않는, 내가 this answer를 참조했습니다

"2000ms의 제한 시간이 초과 오류"

가 : 문제는 테스트가 실패하면 실패 메시지는 것입니다.

올바른 테스트 이름과 실패 이유를 표시하려면 어떻게해야합니까?

NB 2 차 질문은 응답 객체가 개선 될 수 있다고 주장 할 수 있습니까?

답변

14

예외가 어딘가에 삼켜지고있는 것 같습니다. 내 마음에 오는 첫번째 것은 약속 체인의 끝에 done을 추가 :이 같은 방법을 호출하여 체인을 종료 할 약속과 함께 작업 할 때

exports.getProducts = function (request, response) { 
    getProducts() 
     .then(function (data) { 
      response.write(JSON.stringify(data)); 
      response.end(); 
     }) 
     .done(); /// <<< Add this! 
}; 

그것은 일반적으로 경우입니다. 일부 구현에서는 done이라고하고 일부는 end이라고합니다.

How can I get this code to display the correct test name and the reason for failure?

모카는 결코 을 제외하고 볼 수 없다면, 그것은 당신에게 좋은 오류 메시지를 제공 할 수있는 일은 없다. 삼켰을 가능성이있는 예외를 진단하는 한 가지 방법은 문제 코드 주위에 try ... catch 블록을 추가하고 콘솔에 무언가를 버리는 것입니다.

+0

.done() 고정! 나는 그것을 어떻게 놓쳤을 까!? 감사. –

+1

500 이상의 사양을 실행하는 중에이 오류가 발생하는 경우에 대한 아이디어가 있습니까? –

+0

> 예외가 어딘가에서 삼켜지고 있습니다. – givanse

관련 문제