如何在 tdd 中重构测试? [英] How to refactor tests in tdd?

查看:22
本文介绍了如何在 tdd 中重构测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行此 TDD kata 练习:http://osherove.com/tdd-kata-1

I'm performing this TDD kata excercise: http://osherove.com/tdd-kata-1

我生成了以下代码(在这个练习中从 1 到 5 的点 - 我有单元测试):

I produced following code (points from 1 to 5 in this excercise - I have unit tests for it):

public class StringCalculator
{
    private readonly string[] _defaultSeparators = { ",", "\n" };

    public int Add(string numbers)
    {
        // Parser section (string to list of ints)
        var separators = _defaultSeparators;

        var isSeparatorDefinitionSpecified = numbers.StartsWith("//");
        if (isSeparatorDefinitionSpecified)
        {
            var endOfSeparatorDefinition = numbers.IndexOf('\n');

            var separator = numbers.Substring(2, endOfSeparatorDefinition - 2);

            numbers = numbers.Substring(endOfSeparatorDefinition);
            separators = new[] { separator };
        }

        var numbersArray = numbers.Split(separators, StringSplitOptions.RemoveEmptyEntries);
        var numbersArrayAsInts = numbersArray.Select(int.Parse).ToArray();

        // Validator section
        var negativeNumbers = numbersArrayAsInts.Where(c => c < 0).ToArray();
        if (negativeNumbers.Any())
        {
            throw new Exception(string.Format("negatives not allowed ({0})", string.Join(", ", negativeNumbers)));
        }

        return numbersArrayAsInts.Sum();
    }
}

现在我想将代码重构为这样的:

Now I want to refactor code to something like this:

public int Add(string numbers)
{
    var numbersAsInts = CalculatorNumbersParser.Parse(numbers);

    CalculatorNumbersValidator.Validate(numbersAsInts);

    return numbersAsInts.Sum();
}

我应该如何计划重构以正确重构我的代码和单元测试?

How I should plan refactor to properly refactor my code and unit tests?

我认为我应该将部分测试移至新创建的实现类测试(CalculatorNumbersParserTests 和 CalculatorNumbersValidatorTests),更改一些现有测试并添加对 Parse 和 Validate 方法执行的测试.

I think that I should move part of tests to new created implementations classes tests (CalculatorNumbersParserTests and CalculatorNumbersValidatorTests), change some existing tests and add tests for Parse and Validate method execution.

但是在不破坏测试的情况下执行此操作的正确方法是什么?

But what is the correct way to do this without breaking the tests?

推荐答案

我会警告不要移动测试,如果你这样做,那么你的测试就与实现相关联,这意味着它们非常脆弱,所以你必须每次你想改变你的实现时都要改变你的测试.当您拥有庞大的代码库时,这会很快变得昂贵,并可能成为进行更改的阻碍因素.

I would caution against moving the tests as if you do this then your tests are tied to the implementation, which means they are very brittle and so you will have to change your tests every time you want to change your implementation. This can quickly become expensive when you have a large code base and can become a prohibiting factor in making changes.

您现有的测试应指定字符串计算器的行为,因此只要保持所需的行为,您就可以将实现重构为任何内容.

Your existing tests should specify the behaviour of your string calculator and so you can refactor your implementation to anything as long as you keep the desired behaviour.

我倾向于将单元视为行为单元",可能需要几个类来实现.

I tend to think of a unit as a 'unit of behaviour' and it may take a few classes to implement this.

如果您将某些类放置在不同的程序集中,情况可能会发生变化,此时您可能希望在新程序集中进行一些新测试,以确保这些组件的行为不会意外更改,但在在这种情况下,我怀疑你会这样做.

Things might change if you were to place some classes in a different assembly, at which point you would probably want to make some new tests along side the new assembly, to ensure the behaviour of those components is not changed unexpectedly, but in this case I doubt you will be doing this.

如果您开始在多个地方重用类,情况也可能会发生变化,此时您可能需要单独的测试来指定类的行为,而与它们在这些地方的使用无关.

Things might also change if you start to reuse the classes in several places, at which point you may want separate tests to specify the behaviour of the classes independently of their use in the places.

这篇关于如何在 tdd 中重构测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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