使用sinon chai和mocha在nodejs中测试服务

问题描述 投票:0回答:1

我正准备测试我的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 sinon-chai
1个回答
0
投票

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

关于您的第二个问题,这取决于,但是就您的情况而言,hashkey是环境变量,您应该在运行测试之前进行设置,因此不必进行测试。

© www.soinside.com 2019 - 2024. All rights reserved.