0

失敗していたgithubスペックに遭遇し、スペックの書き方を学んでいるときに、最後の1つで失敗していた2つを、コメント#THIS ONE ISSTILLFAILINGで修正しました。どのようにそれを通過させるのでしょうか?

class Team
  attr_reader :players
  def initialize
    @players = Players.new
  end
end

class Players
  def initialize
    @players = ["","Some Player",""]
  end
  def size
    @players.size
  end
  def include? player
    raise "player must be a string" unless player.is_a?(String)
    @players.include? player
  end
end

describe "A new team" do

  before(:each) do
    @team = Team.new
  end

  it "should have 3 players (failing example)" do
    @team.should have(3).players
  end

  it "should include some player (failing example)" do
    @team.players.should include("Some Player")
  end

  #THIS ONE IS STILL FAILING
  it "should include 5 (failing example)" do
    @team.players.should include(5)
  end

  it "should have no players"

end
4

1 に答える 1

0

目的は仕様を変更することであり、仕様に合格するようにコードを変更することではないと仮定します。

この場合、実際@team.playersには 5 を含めることは想定していません。むしろ、非文字列が含まれているかどうかを尋ねられたときに例外が発生することを期待しています。これは次のように記述できます。

  it "should raise an exception when asked whether it includes an integer" do
    expect {
      @team.players.should include(5)
    }.to raise_exception
  end

例外の種類とメッセージを確認するには、 を使用しますraise_exception(RuntimeError, "player must be a string")

他の 2 つの仕様の説明も同様に変更する必要があります。失敗することはなくなったからです。

于 2012-12-28T18:21:55.787 に答える