如何指定一個類與Date進行比較?創建一個與Date類相媲美的Ruby類
class FolderDate
attr_reader :folder
def initialize folder
@folder = folder
end
end
class AsteriskFolderDate < FolderDate
include Comparable
attr_reader :date
def initialize folder
super
@date = get_date
end
def <=>(other)
@date <=> other
end
private
#omitted
end
在Rails控制檯...
AsteriskFolderDate.new("20130628") > Date.today-1
=> true
AsteriskFolderDate.new("20130627") > Date.today
=> false
規格文件...
describe "An instance of", AsteriskFolderDate do
let(:folder) { DateFolder.new(Date.today).asterisk_folder }
subject { AsteriskFolderDate.new(folder) }
#omitted
it "should compare based on @date" do
expect(Date.today-1).to be < subject
expect(subject).to be > Date.today-1
end
end
測試失敗...
Failure/Error: expect(Date.today-1).to be < subject
ArgumentError:
comparison of Date with AsteriskFolderDate failed
控制檯輸出讓我想認爲它正在工作,但測試失敗。我在正確的軌道上,還是測試指出我在控制檯中缺少的缺陷?