Ruby - 使测试通过

Ruby - Making test pass

我正在尝试通过此测试,但不确定如何通过。

测试

def test_it_is_thirsty_by_default
  vampire = Vampire.new("Count von Count")
  assert vampire.thirsty?
end

def test_it_is_not_thirsty_after_drinking
  vampire = Vampire.new("Elizabeth Bathory")
  vampire.drink
  refute vampire.thirsty?
end

代码

def thirsty?
  true
end

def drink
  thirsty? === false
end

它在上次测试时给出失败消息:

Failed refutation, no message given

我错过了什么?我的想法是,最初,吸血鬼口渴(真),然后定义了一个方法,使吸血鬼不口渴(假)。

编辑

即使我将 drink 方法重新分配给:

thirsty? = false

我收到指向 = 符号的语法错误。

您遗漏了一些东西,最重要的是某种编写器方法,它允许您存储 @thirsty 正在您的 drink 方法调用中更新的事实

有几种不同的方法可以做到这一点,但我在下面展示了一种方法并附有一些注释:

require 'test/unit'

class Vampire
  def initialize(name)
    @name = name
    @thirsty = true # true by default
  end

  def drink
    @thirsty = false # updates @thirsty for the respective instance
  end

  def thirsty?
    @thirsty
  end
end

class VampireTests < Test::Unit::TestCase
  def test_it_is_thirsty_by_default
    vampire = Vampire.new("Count von Count")
    assert vampire.thirsty?
  end

  def test_it_is_not_thirsty_after_drinking
    vampire = Vampire.new("Elizabeth Bathory")
    vampire.drink
    refute vampire.thirsty?
  end
end