1

注: 私は 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
4

2 に答える 2

0

きゅうりの問題ではありません。

問題は、exec_cmd で、split が「when」ではなく「case」句でのみ呼び出されることでした。これは、コマンドの形式が「a 1 2 b」であるため、「when」の cmd[1] は、配列の 2 番目の値ではなく、文字列の 2 番目の文字であるスペースを呼び出し、他の関数はそれを to_i に変換し、0 を返します。

exec_cmd を次のように変更しました。

def exec_cmd(cmd = nil)
  if !cmd.nil?
    cmd = cmd.split
    case cmd.first
    when "I" then create_image(cmd[1], cmd[2])
    [...]
end

問題を修正しました。

于 2012-05-14T08:11:59.370 に答える