sanny Sin
sanny Sin

Reputation: 1555

Wrong boolean result comparing Date objects

I'm trying to write test that compare some dates. So far i have 2 tests, one of them works as intended, but the other one fails because doesnt/not correctly compare dates. Here is my code:

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 object

creditcard = ActiveMerchant::Billing::CreditCard.new(
        :number     => 1234567890123456
        :month      => 01,
        :year       => 13,
        :first_name => 'John', 
        :last_name  => 'Doe',
        :verification_value  => 132,
        :brand => 'visa'
      )

Here is output of p's First block works as intended.

"--------"
Tue, 31 Jan 0013
Thu, 28 Feb 2013
false

Second block fails, expecting true, but got false

."--------"
Tue, 31 Jan 0013
Fri, 30 Nov 2012
false

Here is my rspec code

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

in irb it works as charm, returning correct value(true/false)

Upvotes: 1

Views: 237

Answers (1)

AJcodez
AJcodez

Reputation: 34196

I think the problem is in your logic. A card is expired when the expiration date is before the billing date, thus when

card_date < billing_date # expired

and not when

card_date > billing_date # valid

Also try puting in the full 2013 and see if that helps if it keeps breaking

:year => 2013,

You're also missing a comma after this line (probably a copy/paste error) :number => 1234567890123456

Upvotes: 2

Related Questions