2014-04-11 6 views
4

두 개의 테스트 케이스 즉, "..와 전달해야합니다".. 그리고이 테스트는 "2000ms를 초과했습니다. 오류.2000ms의 타임 아웃이 mocha를 초과했습니다

describe("flickrphotoSearch", function() { 
it("should pass with correct inputs", function (done) { 
    flickrApplication.flickrPhotoSearch("hello", "flickr_user_Key", 1, handleData); 
    function handleData(photoUrl, done) { 
     this.setTimeout(1500); 
     assert.isString(photoUrl.toString(), 'not a string'); 
     setTimeout(done, 1000); 
    }; 
}); 
it("should fail with wrong key", function (callingDone) { 
    flickrApplication.flickrPhotoSearch("hello", "wrong key", 1, handleData); 
    function handleData(photoUrl, done) { 
     this.setTimeout(1500); 
     assert.equal(photoUrl.stat, "ok", photoUrl.message); 
     setTimeout(done, 1000); 
    }; 
}); 
}); 

첫 번째 테스트 시간 초과 오류가 발생하지만 두 번째 테스트는 좋은 실행 중입니다. 내가 틀린 곳을 말해주십시오.

답변

1

두 부분으로되어 있습니다. 첫째, 테스트의 타임 아웃을 설정하려고하면 올바른 객체에서 setTimeout 메서드를 호출하지 않습니다. 이는 폐쇄하는 것입니다 : 함수가 아닌 객체 방법으로, 자체적으로 불리는

handleData가 호출
describe("flickrphotoSearch", function() { 
it("should pass with correct inputs", function (done) { 
    # 'this' is the mocha test here. 
    flickrApplication.flickrPhotoSearch("hello", "flickr_user_Key", 1, handleData); 
    function handleData(photoUrl, done) { 
     this.setTimeout(1500); # What's 'this' here? The global object. 
     assert.isString(photoUrl.toString(), 'not a string'); 
     setTimeout(done, 1000); 
    }; 
}); 

, this은 아무것도에 바인딩되지 않습니다. 클로저에 대한 자세한 내용은 this 바인딩을 참조하십시오 (this jQuery Learning Center article 참조).

flickrApplication.flickrPhotoSearch("hello", "flickr_user_Key", 1, handleData.bind(this)); 

그러나이 경우 당신은뿐만 아니라 handleData 외부 this.setTimeout(1500)를 이동할 수 있으며 동일한 효과를 가질 것입니다 : 당신은 수행하여 그를 해결 할 수있다.

다른 부분은 2000ms 제한 시간을 초과하면 1500ms 제한을 초과한다는 것입니다. 또한 이는 flickr API 응답 시간에 따라 결정적이지 않습니다.

내 조언은 이것이 통합 테스트와 달리 단위 테스트 인 경우 flickr API를 조롱하는 것입니다.