2013-07-12 3 views
2

모카를 사용하고 있으며 간단한 데이터베이스 쿼리를 테스트 할 때 간단한 Moongose ​​스키마 함수에 대해 비동기 테스트를 실행하려고 시도하지만 매번 초과 된 시간 초과 오류가 발생합니다.비동기 모카 검사에서 시간 초과 오류가 발생했습니다.

Error: timeout of 15000ms exceeded 
    at null.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:165:14) 
    at Timer.listOnTimeout [as ontimeout] (timers.js:110:15) 

this.timeout (15000)을 사용하고 내가이 오류를 얻고 무엇을 지금까지 타임 아웃 시간을 내가주고 또한 모카 명령하지만 성공의 15000 --timeout 을 시도했습니다. 동기 테스트 만 통과합니다. 아래는 테스트하고 싶은 테스트와 기능입니다.

내 모카 테스트 : -

describe('#getFacility()', function() { 
    this.timeout(15000); 
    it('should get facility successfully', function (done) { 
     db.getFacilites(Data.testFacility, function (err, facilities) { 
      if (err) throw err; 
      facilities.Name.should.equal(Data.testFacility.body.Name); 
      done(); 
     }) 
    }); 
}); 

내 데이터 : -

testFacility : { 
    params: { clientId:"51c85c3267b6fc1553000001" } 
}, 

내 가져 오기 기능이

getFacilites: function (req, res) { 
    Facility.find({Client: req.params.clientId}, function (err, facilities) { 
     if(err) { 
      res.send(500,err); 
     } else if (!facilities) { 
      res.send(404, "facilities not found"); 

     } else if (req.query.format && req.query.format === 'select') { 
      var result = facilities.map (function (f) { 
       return { value: f._id.toString(), text: f.Name } 
      }); 
      res.json(result); 

     } else { 
      console.log("Cannot Retrieve Facilities"); 
     } 
    }); 
} 

내가 EV 쿼리를위한 새로운 별도의 함수를 만들었지 만 아직 작동하지 않습니다. 그 기능은 다음과 같이 보입니다.

describe('#getFacility() direct from DB', function() { 
    it('should get facility successfully from db', function (done) { 
     Client_data.Facility.find({Client: Data.testFacility.params.clientId}, function(err, facilities) { 
      if (err) throw (err); 
      if (facilities) { 
       facilities.forEach(function (f) { 
        console.log(f); 
       }); 
       done(); 
      } 
     }); 
    }); 
}); 

내가하려고하면

는 테스트를 통과, 쿼리 후() 콜백을 수행 호출 할 수 있지만, 그 또한 나에게 좋은 모습이 아니다.

describe('#addFacility()', function() { 
    it('should add facility successfully', function (done) { 
     API_Calls.addFacility(Data.testFacility, function (doc) { 
      doc.Name.should.equal(Data.testFacility.body.Name); 
     }); 
     done(); 
    }); 
}); 

답변

2

귀하의 getFacilities은 REQ, 고해상도를 복용하고 다음 당신은 그것을 테스트에서 완전히 다른 무언가 (A testFacility 객체와 콜백)을 통과하고있다.

getFacilities 메소드 정의가 req, res 및 next, 아마도 clientId 및 next only를 취하지 않아야한다고 생각하며 다음 콜백에 따라 적절한 응답을 생성 할 수 있습니다.

+0

URL에 대한 app.get 요청의 결과로이 함수가 호출 되었기 때문에 나는 req, res 및 next를 사용하고있었습니다. –

+0

@SulemanMirza 그런 다음 경로 처리기로 테스트해야합니다. (아마도 모의)'Request','Response', 그리고''Request.getFacilites ' '함수 (실수)'. 또는 리팩터링을하므로 쿼리의 경우 1) 경로 처리기의 경우 2) 기능이 있으므로 첫 번째 (쿼리)가 예상대로 사용 될 수 있습니다. –

+0

Jonathan을 도와 주셔서 감사합니다. 이제 별도의 쿼리 기능을 사용해 보았지만 여전히 작동하지 않습니다. 여전히 시간 초과가 오류를 초과했습니다. –