gem 'minitest', '~> 5.2'
require 'minitest/autorun'
require 'minitest/pride'
require_relative '../lib/costume'
require_relative '../lib/bag'
require_relative '../lib/candy'
require_relative '../lib/trick_or_treater'
class TrickOrTreaterTest < Minitest::Test
def test_wears_a_costume
costume = Costume.new("Cowboy")
trick_or_treater = TrickOrTreater.new(costume)
assert_equal "Cowboy", trick_or_treater.dressed_up_as
end
def test_wears_a_different_costume
costume = Costume.new("Pony")
trick_or_treater = TrickOrTreater.new(costume)
assert_equal "Pony", trick_or_treater.dressed_up_as
end
class TrickOrTreater
attr_reader :dressed_up_as
def initialize(costume = "Cowboy")
@dressed_up_as = dressed_up_as
end
end
为什么这不能解决前两个测试?我通过了分配给“牛仔”的服装论证,应该照顾第一次测试,并解决第二次。我错过了什么吗?
答案 0 :(得分:2)
我想你会想要更像这样的东西。当你得到一个服装时,你应该将它存储为一个名为服装的实例变量。然后你应该从dress_up_as方法返回服装的类型。无法看到服装课程,所以不知道如何解决这个问题,所以只需要输入.type。
以你自己的方式存储东西的问题是你进入服装对象并且测试似乎想要一个字符串。此外,您打算获得服装对象,但您的默认值是一串牛仔。
class TrickOrTreater
def initialize(costume)
@costume = costume
end
def dressed_up_as
@costume.type
end
end