简体   繁体   中英

Ruby - How do I address arguments in a method? Test will not pass

TDD

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

CODE

class TrickOrTreater
 attr_reader :dressed_up_as

 def initialize(costume = "Cowboy")
  @dressed_up_as = dressed_up_as
 end
end

Why does this not address the first two tests? I passed an argument of costume assigned to "Cowboy", that should take care of the first test and also address the 2nd. Am I missing something?

I think you would want something more like this. As you are getting a costume in you should probably store it as an instance variable called costume. You should then return from the method dressed_up_as the type of the costume. Can't see Costume class so don't know how to get that out so just put .type .

Issue with storing things your way would be that you are getting in a costume object and the test seems to want a string. Also, you are intending to get in costume object but your default value is a string of Cowboy.

class TrickOrTreater

 def initialize(costume)
   @costume = costume
 end

 def dressed_up_as 
   @costume.type
 end

end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM