Given the following Grade
implementation:
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
I'd just test that Grade#<=>
behaves properly, according to what the documentation for Object#<=>
says:
# 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