我假设既然你提到了 TDD,那么有问题的代码实际上并不存在。如果是这样,那么您不是在做真正的 TDD,而是在做 TAD(开发后测试),这自然会导致诸如此类的问题。在 TDD 中,我们从测试开始。看来您正在构建某种类型的菜单或命令系统,所以我将使用它作为示例。
describe GameMenu do
it "Allows you to navigate to character creation" do
# Assuming character creation would require capturing additional
# information it violates SRP (Single Responsibility Principle)
# and belongs in a separate class so we'll mock it out.
character_creation = mock("character creation")
character_creation.should_receive(:execute)
# Using constructor injection to tell the code about the mock
menu = GameMenu.new(character_creation)
menu.execute("c")
end
end
这个测试会导致一些类似于下面的代码(记住,只要足够的代码使测试通过,就不会更多了)
class GameMenu
def initialize(character_creation_command)
@character_creation_command = character_creation_command
end
def execute(command)
@character_creation_command.execute
end
end
现在我们将添加下一个测试。
it "Allows you to display character inventory" do
inventory_command = mock("inventory")
inventory_command.should_receive(:execute)
menu = GameMenu.new(nil, inventory_command)
menu.execute("i")
end
运行此测试将引导我们实现如下实现:
class GameMenu
def initialize(character_creation_command, inventory_command)
@inventory_command = inventory_command
end
def execute(command)
if command == "i"
@inventory_command.execute
else
@character_creation_command.execute
end
end
end
这个实现将我们引向一个关于我们的代码的问题。当输入无效命令时,我们的代码应该怎么做?一旦我们确定了该问题的答案,我们就可以实施另一个测试。
it "Raises an error when an invalid command is entered" do
menu = GameMenu.new(nil, nil)
lambda { menu.execute("invalid command") }.should raise_error(ArgumentError)
end
这推动了对execute 方法的快速更改
def execute(command)
unless ["c", "i"].include? command
raise ArgumentError("Invalid command '#{command}'")
end
if command == "i"
@inventory_command.execute
else
@character_creation_command.execute
end
end
现在我们已经通过了测试,我们可以使用 Extract Method 重构将命令的验证提取到 Intent Revealing Method。
def execute(command)
raise ArgumentError("Invalid command '#{command}'") if invalid? command
if command == "i"
@inventory_command.execute
else
@character_creation_command.execute
end
end
def invalid?(command)
!["c", "i"].include? command
end
现在我们终于可以解决您的问题了。由于 invalid? 方法是通过重构现有的被测代码来驱动的,因此无需为它编写单元测试,它已经被覆盖并且不能独立存在。由于我们现有的测试未对库存和角色命令进行测试,因此需要独立进行测试驱动。
请注意,我们的代码可能会更好,所以在测试通过时,让我们再清理一下。条件语句表明我们违反了OCP(开闭原则),我们可以使用Replace Conditional With Polymorphism重构来移除条件逻辑。
# Refactored to comply to the OCP.
class GameMenu
def initialize(character_creation_command, inventory_command)
@commands = {
"c" => character_creation_command,
"i" => inventory_command
}
end
def execute(command)
raise ArgumentError("Invalid command '#{command}'") if invalid? command
@commands[command].execute
end
def invalid?(command)
!@commands.has_key? command
end
end
现在我们重构了这个类,这样一个额外的命令只需要我们向命令哈希添加一个额外的条目,而不是改变我们的条件逻辑以及invalid? 方法。
所有测试应该仍然通过,我们几乎完成了我们的工作。一旦我们测试驱动各个命令,您就可以返回到初始化方法并为命令添加一些默认值,如下所示:
def initialize(character_creation_command = CharacterCreation.new,
inventory_command = Inventory.new)
@commands = {
"c" => character_creation_command,
"i" => inventory_command
}
end
最后的测试是:
describe GameMenu do
it "Allows you to navigate to character creation" do
character_creation = mock("character creation")
character_creation.should_receive(:execute)
menu = GameMenu.new(character_creation)
menu.execute("c")
end
it "Allows you to display character inventory" do
inventory_command = mock("inventory")
inventory_command.should_receive(:execute)
menu = GameMenu.new(nil, inventory_command)
menu.execute("i")
end
it "Raises an error when an invalid command is entered" do
menu = GameMenu.new(nil, nil)
lambda { menu.execute("invalid command") }.should raise_error(ArgumentError)
end
end
最后的GameMenu 看起来像:
class GameMenu
def initialize(character_creation_command = CharacterCreation.new,
inventory_command = Inventory.new)
@commands = {
"c" => character_creation_command,
"i" => inventory_command
}
end
def execute(command)
raise ArgumentError("Invalid command '#{command}'") if invalid? command
@commands[command].execute
end
def invalid?(command)
!@commands.has_key? command
end
end
希望有帮助!
布兰登