我试图让我对TDD方法学有所了解,并遇到了-我认为是-鸡与蛋的问题:如果错误修复涉及方法签名的更改,该怎么办。
考虑以下方法签名:
string RemoveTokenFromString (string delimited, string token)
顾名思义,此方法从
token
中删除了delimited
的所有实例,并返回结果字符串。稍后我发现此方法存在错误(例如,从字符串中删除了错误的位)。因此,我编写了一个测试用例,描述了发生错误的情况,并确保测试失败。
修复bug时,我发现该方法需要更多信息才能正确完成其工作-并且这部分信息只能作为参数发送(被测试的方法是静态类的一部分)。
那我该怎么办?如果我修复了该错误,这将迫使我更改单元测试-那是“正确的” TDD方法吗?
最佳答案
当发现单元的预期行为发生变化时,轰炸测试绝对没有错。
//Up front
[Test]
public void should_remove_correct_token_from_string()
{
var text = "do.it.correctly..";
var expected = "doitcorrectly";
Assert.AreEqual(StaticClass.RemoveTokenFromString(text, "."), expected);
}
//After finding that it doesn't do the right thing
//Delete the old test and *design* a new function that
//Does what you want through a new test
//Remember TDD is about design, not testing!
[Test]
public void should_remove_correct_token_from_string()
{
var text = "do.it.correctly..";
var expected = "doitcorrectly";
Assert.AreEqual(
StaticClass.RemoveTokenFromString(
text,
".",
System.Text.Encoding.UTF8), expected);
}
//This will force you to add a new parameter to your function
//Obviously now, there are edge cases to deal with your new parameter etc.
//So more test are required to further design your new function
关于unit-testing - 当修复程序涉及更改被测方法签名时,如何使用TDD?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/728747/