对如何为重构以组合其他类的类编写测试感到困惑 [英] Confused on how to write tests for classes that are refactored to compose other classes

查看:26
本文介绍了对如何为重构以组合其他类的类编写测试感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有广泛或彻底地阅读 TDD,但我觉得我确实了解基础知识.

I haven't read widely or thoroughly on TDD, but I feel like I do know the basics.

TDD 的一个要点是您首先编写测试.在计算器程序的情况下,也许一个测试用例如下所示:

One point of TDD is that you write the tests first. In the case of a Calculator program, perhaps one test case looks like this:

public class CalculatorTest {
    public void shouldSumOneAndOneCorrectly() {
        Calculator calc = new Calculator();
        calc.push(new Number(1));
        calc.push(new SumOperator());
        calc.push(new Number(1));
        Number sum = calc.equals();
        assert(sum.equals(new Number(2)));
    }
}

如果我在编写任何实现之前编写上述测试,那么我将面临这个问题:我是在测试 Calculator 类的总和是否正确,还是应该测试 Number 类的总和是否正确?当然,这是假设 Calculator 类将在其实现中的某处调用 number.add(number) 函数.但是我认为您在首先编写测试时不应该考虑实现?

If I write the above test before writing any implementation, then I'm faced with this problem: Am I testing if the Calculator class sums correctly, or should I be testing that the Number class sums correctly? Of course, this is assuming that the Calculator class would be calling a number.add(number) function in its implementation somewhere. But I thought you shouldn't be thinking about implementation when you are writing your tests first?

如果我开始为 Calculator 类编写测试,然后在实现过程中,意识到我的 Calculator 类只是将和坐标委托给组合对象,那么我是否应该为这些组合对象编写测试?我要保留计算器测试吗?

If I start writing tests for a Calculator class, and then during the implementation, realize that my Calculator class simply delegates and coordinates to composed objects, do I then write tests for those composed objects? Do I keep the Calculator tests?

您是否开始在非常高的级别(计算器)编写测试,然后在实现功能并创建组合类时,为这些类编写更多测试?

Do you start writing tests at a very high level (Calculator) and then as you implement functionality and create composed classes, write more tests for those classes?

我希望我说得有点道理.

I hope I'm making some sense.

推荐答案

我会从关注行为开始.

您希望 Calculator 类能够添加 2 个数字.如果您通过其他类执行此操作,它(非常)无关紧要,因为这是计算器的实现细节.如果您在那里实现功能,那么您的测试应该会覆盖 Number 类.

You want your Calculator class to be able to add 2 numbers. Its (pretty) irrelevant if you do this via some other class as that is an implementation detail of the calculator. Your tests should give you coverage of the Number class if you implement the functionality there.

如果您认为 Number 类的功能在 Calculator 之外通常很有用(并且不再是实现细节),那么这就是在这一点上,我会考虑单独为 Number 类编写基于行为的测试,但在此之前,我将依赖于 Calculator 行为的测试.

If you decide that the functionality of the Number class is something which is generically useful outside of the Calculator (and is no longer an implementation detail) then that is the point at which I would consider writing behaviour based tests for the Number class separately, but before that point I would rely on the tests of the Calculator behaviour.

对我来说,这类似于测试委托给私有方法来完成工作的公共方法.您不想为私有方法编写测试,因为它们应该包含在公共方法行为的测试中.

For me this is analogous to tests for a public method which delegates to private methods to get the job done. You don't want to write tests for the private methods as they should be covered by the tests for the behaviour of the public methods.

这篇关于对如何为重构以组合其他类的类编写测试感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆