0
문자열로 사용해야하는 클래스가 있고 항상 비어있는 경우에도 문자열입니다. 객체에는 항상 문자열 표현이 있습니다. 다음은 내 수업의 예입니다to_str 또는 to_s 구현 방법
이assert_equal(
"",
"#{@text}",
"Unchanged class should convert to empty by default"
)
테스트는 위의 실패
class Something
def initialize
@real_string_value = "hello"
end
def to_s
return @real_string_value
end
def to_str
return @real_string_value
end
end
text = Something.new
puts("#{text}") #=> #<Something:0x83e008c>
내가 minitest에서이 테스트를 실행했습니다. 왜 내 수업이 문자열로 변환되지 않습니까?
'hello'를 얻었습니다 : https://ideone.com/ofHJ4m – falsetru
테스트 코드에서'# {text} '대신'# {@ text} "가 필요한 이유는 무엇입니까? – falsetru
이 테스트는 minitest'setup' 메소드에서 작성된 인스턴스 변수에 대해 작동합니다. –