주어진 다음의 Grade
구현 :
class Grade
attr_reader :sort_key
def initialize(sort_key)
@sort_key = sort_key
end
def <=>(other)
return nil unless other.respond_to?(:sort_key)
@sort_key <=> other.sort_key
end
end
난 그냥 Object#<=>
에 대한 documentation의 말씀에 따라, Grade#<=>
가 제대로 작동하는지 테스트 것 :
# comparable_grade.spec
describe "Grade#<=>" do
it "returns 0 when both operands are equal" do
(Grade.new(0) <=> Grade.new(0)).should eq(0)
end
it "returns -1 when first operand is lesser than second" do
(Grade.new(0) <=> Grade.new(1)).should eq(-1)
end
it "returns 1 when first operand is greater than second" do
(Grade.new(1) <=> Grade.new(0)).should eq(1)
end
it "returns nil when operands can't be compared" do
(Grade.new(0) <=> Grade.new("foo")).should be(nil)
(Grade.new(0) <=> "foo").should be(nil)
(Grade.new(0) <=> nil).should be(nil)
end
it "can compare a Grade with anything that respond_to? :sort_key" do
other = double(sort_key: 0)
(Grade.new(0) <=> other).should eq(0)
end
end
내가 이길 것 시험을 치르기 전에 그런 사소한 방법을 쓰고 자하십시오. 중요한 것은 테스트를하는 것입니다. 테스트에 반드시 도착해야하는 것은 아닙니다. – tadman
비록 사소한 것이라 할지라도 나는 테스트를 할 때 초보자로서 사람들이 테스트하는 방법을 여전히보고 싶습니다. –
제가 말하고자하는 것은 여러분이 방법을 썼다는 것입니다. 큰 응용 프로그램을 작성하고 테스트를 작성하려고 시도하는 것과는 크게 다르지 않습니다. – tadman