Javascript 测试封闭函数

Javascript 测试封闭函数,javascript,unit-testing,testing,Javascript,Unit Testing,Testing,我目前有两个功能,每个功能都带有测试: function A(x) { // do A things return Aresults; } function testA() { // this put A through its test and make sure it does what it's supposed to } 及 我现在意识到A的唯一用途是在B中。因此我想重新考虑将代码隔离和保护到: function B(x) { function A(x) { /

我目前有两个功能,每个功能都带有测试:

function A(x) {
  // do A things
  return Aresults;
}
function testA() {
  // this put A through its test and make sure it does what it's supposed to
}

我现在意识到A的唯一用途是在B中。因此我想重新考虑将代码隔离和保护到:

function B(x) {
  function A(x) {
    // do A things
    return Aresults;
  }
  // do B things which involve using A
  return Bresults;
}
function testB() {
  // this put B through its test and make sure it does what it's supposed to
}

我的问题是,既然A是B的闭合函数,我怎么能为A添加测试呢?i、 e.我的testA函数去哪里了?

您不需要这样做。单元测试的目的是测试行为而不是实现

所以你测试的是什么方法,而不是它是如何做到的

从这个角度来看,您应该只关心函数调用产生的副作用,就是这样。

看起来很相似,为什么我在询问之前找不到这个?
function B(x) {
  function A(x) {
    // do A things
    return Aresults;
  }
  // do B things which involve using A
  return Bresults;
}
function testB() {
  // this put B through its test and make sure it does what it's supposed to
}