测试关闭功能

时间:2013-03-14 03:17:25

标签: 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
}

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功能在哪里?

1 个答案:

答案 0 :(得分:0)

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

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

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