如何使用Nodejs Lab模块测试此API?

时间:2015-08-12 10:39:29

标签: javascript node.js unit-testing hapijs

任何人都可以帮我这个测试吗?如何为nodejs HAPI模块编写的此API编写单元测试。为了测试,我必须使用Lab模块。 这是route.js文件:

path: '/makes',
method: 'GET',
config: {
    handler: cars.getMakes

这是处理程序:

getMakes: function (request, reply) {

Make //this is a model of moongose in my DB
  .find()
  .sort('name')
  .exec( function(err, result) {

      if (err) {
         request.log(['server', 'database', 'error'], 'An error occurred during the execution of the query');
     }

     return reply(result)
        .code(200);
    });
 },

我自己写了但是我有这样的错误:

  getaddrinfo ENOTFOUND undefined
  at errnoException (dns.js:44:10)
  at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:94:26)

1 个答案:

答案 0 :(得分:0)

当我想测试API时,我通常会编写请求我的API服务器(在测试模式下启动)的旁边代码(使用mocha和expect.js)并控制每种可能情况的答案。

对于你的/makes死记硬背,我只是控制当没有Make时会发生什么,以及它们是否按我希望的那样正确排序。