【问题标题】:How can i Test this API with Nodejs Lab module?我如何使用 Nodejs Lab 模块测试这个 API?
【发布时间】:2015-11-04 22:06:45
【问题描述】:

谁能帮我做这个测试?如何为 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)

【问题讨论】:

  • Stack Overflow 不是代码编写服务。
  • @GergoErdosi 我不需要写入服务,我已经编写了它,但是在 GetAddrInfoReqWrap.onlookup [as oncomplete] 的 errnoException (dns.js:44:10) 处出现错误 getaddrinfo ENOTFOUND undefined (dns.js:94:26)

标签: javascript node.js unit-testing hapijs


【解决方案1】:

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

对于您的/makes 死记硬背,我只需控制在没有Make 时会发生什么,以及它们是否按照我想要的方式正确排序。

【讨论】:

  • 谢谢,但如果我必须控制返回码 200,我该怎么办?
  • 根据您使用的 HTTP 请求模块,您只需确保返回码为 200。我通常使用superagent,并且我的响应对象中有一个statusCode 变量。
猜你喜欢
  • 1970-01-01
  • 2015-08-02
  • 1970-01-01
  • 2016-05-26
  • 1970-01-01
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 2023-01-20
相关资源
最近更新 更多