我正在尝试编写比较某些日期的测试。到目前为止,我有 2 个测试,其中一个按预期工作,但另一个失败,因为没有/没有正确比较日期。这是我的代码:
def self.has_expired?(card, start_month, start_year, annually)
card_date = Date.new(card.year, card.month, -1)
billing_date = Date.new(start_year, start_month, -1)
if !annually
p '--------'
p card_date
p billing_date
card_date > billing_date
else
#return false
end
end
信用卡对象
creditcard = ActiveMerchant::Billing::CreditCard.new(
:number => 1234567890123456
:month => 01,
:year => 13,
:first_name => 'John',
:last_name => 'Doe',
:verification_value => 132,
:brand => 'visa'
)
这是 p 的第一个块按预期工作的输出。
"--------"
Tue, 31 Jan 0013
Thu, 28 Feb 2013
false
第二个块失败,期待真,但得到假
."--------"
Tue, 31 Jan 0013
Fri, 30 Nov 2012
false
这是我的 rspec 代码
describe CreditCard do
context 'card_expired' do
it 'should return false with args passed to method (02month, 13 year, annually==false)' do
CreditCard.has_expired?(creditcard, 02, 2013, false).should == false
end
it 'should return true with args passed to method (11month, 12 year, annually==false)' do
CreditCard.has_expired?(creditcard, 11, 2012, false).should == true
end
end
end
在 irb 它作为魅力,返回正确的值(真/假)