Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mocha/Sinon-JavaScript中的单元测试承诺_Javascript_Unit Testing_Mocha.js - Fatal编程技术网

Mocha/Sinon-JavaScript中的单元测试承诺

Mocha/Sinon-JavaScript中的单元测试承诺,javascript,unit-testing,mocha.js,Javascript,Unit Testing,Mocha.js,在学习electron的过程中,我决定也要培训JavaScript测试技术。我有以下代码: const winston = require('winston'); const AutoLaunch = require('auto-launch'); const launchFunction = (mb) => { const autolaunch = new AutoLaunch(); autolaunch .isEnabled() .then((isEnabl

在学习electron的过程中,我决定也要培训JavaScript测试技术。我有以下代码:

const winston = require('winston');
const AutoLaunch = require('auto-launch');

const launchFunction = (mb) => {
  const autolaunch = new AutoLaunch();

  autolaunch
    .isEnabled()
    .then((isEnabled) => {
      if (isEnabled) {
        return;
      }
      autolaunch.enable();
    })
    .catch((err) => {
      winston.error(err);
    });
};
我想断言在特定条件下是否正确触发了autolaunch.enabled(),并且我在编写任何测试时遇到了很多问题,这些测试不会强迫我使用then()中函数的精确副本创建存根。这个解决方案的设计中有一个选项可能有问题——我可以(并且愿意)更改代码,使其更易于测试。我应该如何在不影响代码可测试性的情况下处理这个问题


我用的是摩卡咖啡和西农咖啡,但我并不喜欢这些工具,我会尝试更实用的方法。在函数中封装有问题的闭包并单独测试它

function enableWhenNeeded(autolaunch, isEnabled) {
      if (isEnabled) {
        return;
      }
      autolaunch.enable();
}

autolaunch
    .isEnabled()
    .then(curry(enableWhenNeeded, autolaunch))
    .catch((err) => {
      winston.error(err);
    });
出于示例的目的,我编写了函数curry(),但我认为至少有35个JavaScript框架提供了一个函数

这段代码是否值得测试一直是个问题;如果AutoLaunch是第三方,为什么要测试它?

请查看测试承诺。