0

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

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
}

function B(x) {
  // 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 中。所以我想重新考虑以隔离和保护代码:

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 的封闭函数,我该如何添加测试?即我的 testA 函数在哪里?

4

1 回答 1

0

你不需要。单元测试的目的是测试行为而不是实现

所以你测试的是什么方法,而不是它是怎么做的。

从这个角度来看,您应该只关心函数调用产生的副作用,仅此而已。

于 2013-03-14T03:21:47.020 回答