首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >红宝石试题

红宝石试题
EN

Stack Overflow用户
提问于 2019-06-03 02:41:52
回答 1查看 119关注 0票数 1

我正试着通过这次考试,我不知道该如何通过考试。

测试

代码语言:javascript
复制
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

代码语言:javascript
复制
def thirsty?
  true
end

def drink
  thirsty? === false
end

它在上一次测试中给出了一个失败的消息:

Failed refutation, no message given

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

编辑

即使我将饮料方法重新分配到:

thirsty? = false

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-03 03:47:10

您缺少了一些东西,最重要的是某种编写方法,它允许您将@thirsty更新到drink方法调用中

有几种不同的方法可以做到这一点,但我在下面向您展示了一种方法,并给出了一些注意事项:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56420576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档