【问题标题】:Testing closed over functions测试封闭函数
【发布时间】:2013-03-02 06:55:00
【问题描述】:

我目前有两个函数,每个函数都有测试:

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 函数在哪里?

【问题讨论】:

  • 看起来类似于link
  • link 为什么在我问之前我找不到这个?

标签: javascript unit-testing testing


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    相关资源
    最近更新 更多