注: 私は TDD とキュウリを初めて使用するので、答えは非常に簡単かもしれません。
テスト用の基本的な画像エディターを作成しています (画像は単なる一連の文字です)。私はキュウリの物語を書きました:
Scenario Outline: edit commands
Given I start the editor
And a 3 x 3 image is created
When I type the command <command>
Then the image should look like <image>
ステップ
Scenarios: colour single pixel
| command | image |
| L 1 2 C | OOOCOOOOO |
常に失敗し、戻る
expected: "OOOCOOOOO"
got: " OOOOOOOO" (using ==) (RSpec::Expectations::ExpectationNotMetError)
これはステップコードです:
When /^I type the command (.*)$/ do |command|
@editor.exec_cmd(command).should be
end
プログラム内の関数 exec_cmd がコマンドを認識し、適切なアクションを起動します。この場合、次のものが起動されます
def colorize_pixel(x, y, color)
if !@image.nil?
x = x.to_i
y = y.to_i
pos = (y - 1) * @image[:columns] + x
@image[:content].insert(pos, color).slice!(pos - 1)
else
@messenger.puts "There's no image. Create one first!"
end
end
ただし、プログラム自体の関数で 2 つのローカル変数 (pos と color) の値をハードコーディングしない限り、これは常に失敗します。
なんで?プログラム自体で何か間違ったことをしているようには見えません。関数は本来の機能を実行し、これらの 2 つの変数はローカルでのみ使用できます。ですから、これはキュウリの使用に問題があると思います. これを適切にテストするにはどうすればよいですか?
- -編集 - -
def exec_cmd(cmd = nil)
if !cmd.nil?
case cmd.split.first
when "I" then create_image(cmd[1], cmd[2])
when "S" then show_image
when "C" then clear_table
when "L" then colorize_pixel(cmd[1], cmd[2], cmd[3])
else
@messenger.puts "Incorrect command. " + "Commands available: I C L V H F S X."
end
else
@messenger.puts "Please enter a command."
end
end