亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

非導出函數的 Mocha 單元測試返回“xx 不是函數”

非導出函數的 Mocha 單元測試返回“xx 不是函數”

天涯盡頭無女友 2022-12-29 10:28:54
我正在嘗試使用 mocha 對非導出函數運行單元測試,但它給出了錯誤“xx 不是函數”。示例結構就像 ff 代碼,其中我想測試函數 isParamValid。settings.js 中的代碼格式已經存在于我們的系統中,所以我無法重構它。// settings.jsconst settings = (() => {  const isParamValid = (a, b) => {    // process here  }  const getSettings = (paramA, paramB) => {    isParamValid(paramA, paramB);  }    return {    getSettings,  }})();module.exports = settings;我試過 ff 代碼來測試它,但是 mocha 給出了錯誤 ReferenceError: isParamValid is not defined// settings.test.jsconst settings= rewire('./settings.js');describe('isParamValid', () => {    it('should validate param', () => {      let demo = settings.__get__('isParamValid');      expect(demo(0, 1)).to.equal(true);      expect(demo(1, 0)).to.equal(true);      expect(demo(1, 1)).to.equal(false);    })  })
查看完整描述

1 回答

?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

你不能直接訪問isParamValid這里。嘗試通過集成對其進行測試,如下所示


const settings = require('./settings.js'); // No need of rewire


describe('isParamValid', () => {

    it('should validate param', () => {

      const demo = settings.getSettings; // Read it from getSettings


      expect(demo(0, 1)).to.equal(true);

      expect(demo(1, 0)).to.equal(true);

      expect(demo(1, 1)).to.equal(false);

    })

})


查看完整回答
反對 回復 2022-12-29
  • 1 回答
  • 0 關注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號