简体   繁体   English

JavaScript —单元测试子任务

[英]JavaScript — Unit Testing Subtasks

They say: 他们说:

"You should test the interface, not the implementation." “您应该测试接口,而不是实现。”

In other words, you should be concerned with the end result, not with how it is accomplished ( black box testing ). 换句话说,您应该关注最终结果,而不是关注如何完成( 黑盒测试 )。

It is also said that you should not test private functions , rather only the public interface that is exposed. 这也是说,你应该测试私有函数 ,而只有在暴露的公共接口。 But my question is... 但是我的问题是

What do you do about a publicly exposed interface (eg, function) that is dependent upon several subtasks that are private? 您如何处理依赖于几个私有子任务的公开接口(例如功能)? How should you go about testing that? 您应该如何进行测试?

Consider the below function calculateDiscountedPrice . 考虑下面的函数calculateDiscountedPrice Let's pretend that the first function is publicly available (think export default) and the other 3 are private. 我们假设第一个函数是公开可用的(请考虑导出默认值),而其他三个是私有的。

// PUBLIC TOP-LEVEL FUNCTION
export default function calculateDiscountedPrice(price, discount) {
    const dollarsOff = getDollarsOff(price, discount);
    return round(price - dollarsOff);
}

// PRIVATE SUBTASK
function getDollarsOff(price, discount) {
    return price * discount;
}

// PRIVATE SUBTASK
function round(number, precision = 2) {
    return isInt(number) 
    ? number
    : number.toFixed(precision);
}

// PRIVATE SUBTASK
function isInt(number) {
    return number % 1 === 0;
}

Example usage: 用法示例:

console.log(calculateDiscountedPrice(100, 0.75)) // 25

As you can see, calculateDiscountedPrice is the public function we are exposing, so we should unit test that. 如您所见, calculateDiscountedPrice是我们要公开的公共函数,因此我们应该对其进行单元测试。 But what about the three other subtasks? 但是其他三个子任务呢? Why shouldn't we test those? 我们为什么不应该测试那些? Would the tests that cover calculateDiscountedPrice cover the other three as well? 涵盖calculateDiscountedPrice的测试是否也calculateDiscountedPrice涵盖其他三个?

You're right that you should not test private functions separately. 没错,您不应该单独测试私有功能。

When you write tests for publicly available code you should consider as much possible branching in your code as possible - so then your tests will touch all the private functions as well. 在为公开代码编写测试时,应考虑代码中尽可能多的分支-因此测试也将涉及所有私有函数。

Furthermore strictly saying you don't need to write tests for all the publicly available functions, otherwise you would test the implementation as well. 此外,严格地说,您不需要为所有公共可用功能编写测试,否则您也将测试实现。

So for this particular example you can write tests like: 因此,对于此特定示例,您可以编写如下测试:

  • pass price to a whole number like 10 to go through a "negative" result of invoking isInt (to be precise - there would be a number result of 0) price传递给整数(例如10以通过调用isInt “负数”结果(准确地说,数字结果为0)
  • pass price to a not whole number like 7.35 to go through a "positive" result of invoking isInt price传递给非整数(例如7.35以通过调用isInt的“正”结果
  • pass zero discount 通过零discount
  • pass non-zero discount 通过非零discount
  • use combinations of previous variants etc. 使用以前的变体等的组合

Let me notice that if you would use TDD technique from the beginning the changes are you'd got these test cases essentially. 让我注意到,如果您从一开始就使用TDD技术,那么所做的更改本质上就是这些测试用例。

Let me also suggest you to spend few minutes and read this article of Uncle Bob who is known professional in software engineering. 让我也建议您花几分钟时间阅读鲍勃叔叔的这篇文章 ,他是软件工程领域的知名专家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM