【问题标题】:testing services in nodejs with sinon chai & mocha使用 sinon chai 和 mocha 在 nodejs 中测试服务
【发布时间】:2020-03-29 14:34:20
【问题描述】:

我正在尝试测试我的 bycrptSevice... 这是我的服务:

module.exports = (bcrypt) => {
  const hashKey = Number(process.env.BCRYPT_HASH_KEY) || 10;

  function checkPassword(reqPassword, userPassword) {
    console.log("bycrptService: checkPassword call()");

    return bcrypt.compareSync(reqPassword, userPassword);
  }

  function createHashPassword(password) {
    console.log("bycrptService: createHashPassword call()");

    return bcrypt.hashSync(password, hashKey);
  }

  return {
    checkPassword,
    createHashPassword
  };
}

这是测试文件:

const { assert, should, expect, sinon } = require('../baseTest');

const bcryptService = require('../../services/bcryptService');
const bcryptjs = require('bcryptjs');

describe('bcryptService Tests', function() {
    const bcrypt = bcryptService(bcryptjs);
    let Password = '1234';
    let crptPassword = bcrypt.createHashPassword(Password);

    it('test the createHashPassword() create new hash password to the input password',function(){
      expect(crptPassword).to.not.be.equal(Password);
    })

    it('test the checkPassword() check if return true when its compere and false when its not', function() {
       bcrypt.checkPassword(Password,crptPassword).should.be.true;
       bcrypt.checkPassword('987',crptPassword).should.be.false;
    })
    it('test onInit bycrptSevice shoud have hashKey',function(){
     //how to check it??? 
    })
});

我的第一个问题是:我如何检查 hashKey 是否存在? 其次:我应该测试它吗?我的意思是 - 我有责任检查它,或者我不关心私人领域
谢谢

【问题讨论】:

    标签: node.js mocha.js sinon-chai


    【解决方案1】:

    为了测试 hashKey,您必须将其导出到 bycrptSevice,然后您可以对其进行任何测试。

    关于你的第二个问题,这取决于你的情况,但对于你的情况,hashkey 是一个环境变量,你应该在运行测试之前设置它,所以没有必要测试它。

    【讨论】:

      猜你喜欢
      • 2017-10-08
      • 1970-01-01
      • 2015-12-01
      • 2018-04-08
      • 1970-01-01
      • 2015-05-16
      • 2020-10-11
      • 1970-01-01
      • 2023-04-11
      相关资源
      最近更新 更多