【发布时间】:2011-05-31 05:29:19
【问题描述】:
最近我写了一个 Ruby 程序来确定"Scramble Squares" 拼图的解决方案:
我使用 TDD 来实现其中的大部分内容,导致测试看起来像这样:
it "has top, bottom, left, right" do
c = Cards.new
card = c.cards[0]
card.top.should == :CT
card.bottom.should == :WB
card.left.should == :MT
card.right.should == :BT
end
这适用于较低级别的“辅助”方法:识别图块的“边”,确定图块是否可以有效地放置在网格中,等等。
但我在编写解决难题的实际算法时遇到了问题。由于我不知道问题的有效可能解决方案,我不知道如何先编写测试。
我最终写了一个非常丑陋的未经测试的算法来解决它:
def play_game
working_states = []
after_1 = step_1
i = 0
after_1.each do |state_1|
step_2(state_1).each do |state_2|
step_3(state_2).each do |state_3|
step_4(state_3).each do |state_4|
step_5(state_4).each do |state_5|
step_6(state_5).each do |state_6|
step_7(state_6).each do |state_7|
step_8(state_7).each do |state_8|
step_9(state_8).each do |state_9|
working_states << state_9[0]
end
end
end
end
end
end
end
end
end
所以我的问题是:当您还不知道有效输出时,如何使用 TDD 编写方法?
如果你有兴趣,代码在 GitHub 上:
【问题讨论】: