Ruby 如何通过此测试,I';我有点困惑 这是我的金钱课 我考试不及格

Ruby 如何通过此测试,I';我有点困惑 这是我的金钱课 我考试不及格,ruby,Ruby,我不知道如何修改amount方法以使测试通过。。。任何帮助都将不胜感激。我想你需要改变 assert_equal "You can't spend what you don't have", money.spend(12) 到 我认为你需要改变 assert_equal "You can't spend what you don't have", money.spend(12) 到 当账户没有足够的钱花时,你应该提出错误 class Money class InsufficientFun

我不知道如何修改amount方法以使测试通过。。。任何帮助都将不胜感激。

我想你需要改变

assert_equal "You can't spend what you don't have", money.spend(12)


我认为你需要改变

assert_equal "You can't spend what you don't have", money.spend(12)


当账户没有足够的钱花时,你应该提出错误

class Money
  class InsufficientFunds < StandardError; end

  attr_accessor :amount

  def initialize
    self.amount = 0
  end

  def earn(this_many)
    self.amount += this_many
  end

  def spend(this_many)
    raise InsufficientFunds, "You can't spend what you don't have" if amount < this_many
    self.amount -= this_many
  end

end

当账户没有足够的钱花时,你应该提出错误

class Money
  class InsufficientFunds < StandardError; end

  attr_accessor :amount

  def initialize
    self.amount = 0
  end

  def earn(this_many)
    self.amount += this_many
  end

  def spend(this_many)
    raise InsufficientFunds, "You can't spend what you don't have" if amount < this_many
    self.amount -= this_many
  end

end

我的代码一直工作到这一行。。。这是一个来自exercism.io的测试,我试图让它通过,但我被卡住了。。。我试图建立一个完整的方法,但我打破了一切..我的代码工作,直到这一行。。。这是一个来自exercism.io的测试,我试图让它通过,但我被卡住了。。。我试着做一个完整的方法,但我打破了一切…谢谢!!这么多。。我尝试了类似的方法,但没有使用RAISE。在我看来,一个方法永远不应该返回不同类型的值(除了
nil
)。你的
Money#spend
应该总是返回一个数字,而不是字符串。这是一种典型的使用错误的情况;结束,以便在整个应用程序中更容易、更明确地挽救此错误。然后,测试将是
assert\u raise InsufficientFunds{{#…}
@engineerinesmnky Agree。更新了我的答案。上一次更新是为了回应问题中的讨论,如果太极端,请随时回滚,@Aetherus.谢谢!!这么多。。我尝试了类似的方法,但没有使用RAISE。在我看来,一个方法永远不应该返回不同类型的值(除了
nil
)。你的
Money#spend
应该总是返回一个数字,而不是字符串。这是一种典型的使用错误的情况;结束,以便在整个应用程序中更容易、更明确地挽救此错误。然后,测试将是
assert\u raise InsufficientFunds{{#…}
@engineerinesmnky Agree。更新了我的答案。之前的更新是针对问题中的讨论进行的,如果太极端,请随意回滚,@Aetherus.这似乎不准确,我很难遵循逻辑,因为你没有为你的推理提供任何解释这似乎不准确,我很难遵循逻辑,因为你没有为你的推理提供任何解释
class Money
  class InsufficientFunds < StandardError; end

  attr_accessor :amount

  def initialize
    self.amount = 0
  end

  def earn(this_many)
    self.amount += this_many
  end

  def spend(this_many)
    raise InsufficientFunds, "You can't spend what you don't have" if amount < this_many
    self.amount -= this_many
  end

end
def test_cant_spend_money_that_you_dont_have
  money = Money.new
  money.earn(75)
  money.spend(75)
  assert_raise Money::InsufficientFunds, "You can't spend what you don't have" do
    money.spend(12)
  end
  assert_equal 0, money.amount
end