本文介绍了Ruby - 使测试通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make this test pass and am not sure how to go about making this 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

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

It is giving a failure message on the last test:

反驳失败,没有给出消息

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

What am I missing? My thought is that initially, the vampire is thirsty(true) and then defined a method that would then make the vampire not thirsty(false).

编辑

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

Even if I reassign the drink method to:

渴了吗?= 假

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

I get a syntax error pointing to the = sign.

推荐答案

您缺少一些东西,最重要的是某种编写器方法,它允许您存储 @thirsty 是在您的 drink 方法调用

You're missing a couple things, most importantly some kind of writer method that allows you to store that fact that @thirsty is getting updated inside of your drink method call

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

There's a couple different ways to do this but I've shown one below with a few notes:

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

这篇关于Ruby - 使测试通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 17:07
查看更多