如何指定一个类与日期相比较?
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
控制台输出让我想认为它正在工作,但测试失败了。我在正确的轨道上,还是测试指出了我在控制台中缺少的缺陷?