这个规范将如何通过

时间:2012-12-28 18:08:43

标签: ruby rspec

我遇到了一个失败的github规范,当我正在学习如何编写规范时,我修复了其中两个失败的最后一个用注释#THIS ONE仍然失败。如何让它通过?

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

1 个答案:

答案 0 :(得分: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")

您应该类似地修改其他两个规范的描述,因为它们不再失败。

相关问题