【问题标题】:Test nodejs methode with Mocha and chai libraries使用 Mocha 和 chai 库测试节点 js 方法
【发布时间】:2019-05-17 23:19:02
【问题描述】:

我想在下面用 Mocha 和 chai 测试这个方法,但是我得到了一个错误: 我想用 Mocha 和 chai 在下面测试这个方法,但是我得到了一个错误:

exports.getCaracteristiques = (req,res) => {
  db.query('SELECT "titi", "toto"')
.then( ({rows}) => {
  var caracteristiqueResult = rows.map((row) => {
    return {
      'idCaracteristique': row.CARACT_ID
      , 'libelleCaracteristique': row.toto
      , 'libelleCaracteristique': row.titi

    };
  })
  res.json(caracteristiqueResult);
})
.catch(err => {
  // handle the error
  console.log("ERROR :", err)
  res.status(500).end(err)
})
   };

test.js 文件包含:

var expect = require('chai').expect; 
require("../config/config");


var ctr = require('../api/controllers/caracteristiques')

describe('Caracteristiques', () => {

it('returns an array of Carateristiques', () => {
    // This will fail if "Caracteristiques result "is
    // not  array.

    return ctr.getCaracteristiques.then(function(data){
        expect(data).to.be.a('array');

    });// no catch, it'll figure it out since the promise is rejected

  });
   })

但我收到了这个错误:

 Caracteristiques
1) returns an array of Carateristiques


 0 passing (0ms)
 1 failing

 1) Caracteristiques
   returns an array of Carateristiques:
     TypeError: ctr.getCaracteristiques.then is not a function
    at Context.it (test\caracteristiques.js:13:40)

如何解决这个问题?

【问题讨论】:

    标签: node.js mocha.js chai


    【解决方案1】:

    错误:

    TypeError: ctr.getCaracteristiques.then is not a function
    

    完全正确。在调用getCaracteristiques() 方法时,您错过了()

    这应该可行:

    return ctr.getCaracteristiques().then(function(data){
        expect(data).to.be.a('array');
    });
    

    在 OP 评论后编辑:

    在您的代码中,getCaracteristiques 需要一个 req 和一个 res 对象。我的猜测是您使用 express 或任何其他节点 js http 库,这通常会为您填充。

    在 mocha 中,我们是自己调用方法,没有任何网络服务器,所以我们需要自己制作这些对象。

    getCaracteristiques 方法不使用req,但它确实需要res 才能拥有json() 方法。所以我们可以这样做:

    const res = {
      json: (a) => a,
    };
    
    return ctr.getCaracteristiques({}, res).then(function(data){
        expect(data).to.be.a('array');
    });
    

    在我们的测试中,getCaracteristiques 返回我们想要的内容(只是数据,因为伪造的 json 方法只是放弃了它的参数)。

    现在,测试将再次失败,因为getCaracteristiques 没有返回json调用。如果我们添加 return 语句,整个事情应该可以工作:

    exports.getCaracteristiques = (req,res) => {
      return db.query('SELECT "CARACT_ID", "CARACT_LIB", "COULEUR", "IMAGE_ACTIVE", 
      "IMAGE_PASSIVE", "ACTIF" FROM "DIM_PRC_CARACT"')
      .then( ({rows}) => {
      var caracteristiqueResult = rows.map((row) => {
        return {
          'idCaracteristique': row.CARACT_ID
          , 'libelleCaracteristique': row.CARACT_CD
          , 'libelleCaracteristique': row.CARACT_LIB
          , 'couleurCaracteristique': row.COULEUR
          , 'pictogrammeActifCaracteristique': row.IMAGE_PASSIVE
          , 'pictogrammePassifCaracteristique': row.IMAGE_PASSIVE
        };
      })
      return res.json(caracteristiqueResult);
    })
    

    请注意,return 已添加到 db.queryres.json,因此 getCaracteristiques 方法现在返回特征的承诺。

    【讨论】:

    • 谢谢你是对的,但很明显,我得到了另一个错误:错误:TypeError:无法读取 db.query.then 处未定义的属性 'json'(C:\Users\zya\Documents\Tdbc- api\tdbc-api\api\controllers\caracteristiques.js:18:11)
    • 你能帮忙吗,因为这是我第一次用 Mocha 做单元测试
    • 当我把这行 res.json(caracteristiqueResult);在评论中我得到了另一个错误: Caracteristiques 返回一个 Carateristiques 数组:TypeError: Cannot read property 'then' of undefined at Context.it (test\caracteristiques.js:13:41)
    • 更新了答案,希望对你有帮助
    • 非常感谢您按预期工作!我有一个问题,为什么我们使用两个回报?
    猜你喜欢
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 2013-09-26
    • 2018-02-27
    相关资源
    最近更新 更多