Odd Date-based spec failing

Hrm.

I have a spec for a model ‘Phase’. a Phase has a start and end date,
and a method ‘percentage_complete’ that calculates the percentage of
time that has already elapsed between said start and end dates.

This spec passes as expected:

module PhaseHelper
def valid_attributes
{
:start_date => Date.today - 5.days,
:end_date => Date.tomorrow
}
end
end

describe Phase do

include PhaseHelper

before(:each) do
@phase = Phase.new(valid_attributes)
end

it “should know how to generate the percentage_complete attribute” do
@phase.percentage_complete.should == 83
end

However, when I run ‘rake spec:models’, the spec fails:

‘Phase should know how to generate the percentage_complete attribute’
FAILED
expected: 83,
got: 100 (using ==)

RSpec and rspec_on_rails are current from GitHub (RSpec gem has been
reinstalled from source)

It seems your Phase.percentage_complete is not working as you expect.
It clearly returns 100 when you are specting 83. The test clearly said
that.

¿What is the implementation of your percentage_complete method?

2008/7/1 Matt D. [email protected]:

On Jul 1, 2008, at 10:10 PM, Camilo T. wrote:

It seems your Phase.percentage_complete is not working as you expect.
It clearly returns 100 when you are specting 83. The test clearly said
that.

¿What is the implementation of your percentage_complete method?

It only fails when run via ‘rake spec:models’; otherwise, it passes.

def percentage_complete
# Returns an integer representation of a percentage (i.e. ‘78’)

 return 100 if Date.today >= self.end_date

 length_in_days  = (self.end_date - self.start_date).to_f
 days_into_phase = (Date.today - self.start_date).to_f

 ((days_into_phase / length_in_days) * 100.0).round

end

What do you see when you puts the values of end_date, start_date, and
Date.today inside of #percentage_complete?

On Jul 1, 2008, at 2:10 AM, Pat M. wrote:

What do you see when you puts the values of end_date, start_date, and
Date.today inside of #percentage_complete?

When run via TextMate/Rspec Bundle:
Date.today = 2008-07-01
self.start_date = 2008-06-26
self.end_date = 2008-07-02

When run via ‘rake spec:models’
Date.today = 2008-07-01
self.start_date = 0825-09-17
self.end_date = 2008-07-02

Pretty obvious what the bug is suddenly, but would cause this?
Come to think of it, I’d seen this before in rare random cases in
other Date-based specs in the past, though currently everything else
is passing.

On Jul 1, 2008, at 8:23 AM, Tom S. wrote:

$ ./script/console

Date.today.minus_with_duration(5.days)
=> Thu, 26 Jun 2008

Date.today.minus_without_duration(5.days.to_i)
=> Sun, 17 Sep 0825

Is this a load-order problem? Perhaps
ActiveSupport::CoreExtensions::date::Calculations isn’t kicking in
correctly.

Hrm, quite interesting there. So would this be considered a Rails issue?

On Jul 1, 11:21 am, Matt D. [email protected] wrote:

correctly.
Anyone else have thoughts on this?

On Jul 3, 2008, at 8:27 AM, Matt D. wrote:

ActiveSupport::CoreExtensions::date::Calculations isn’t kicking in
correctly.
Anyone else have thoughts on this?

Yeah - use the debugger (ruby-debug) and the failing test case:

it “should have 5.days as the proper fixnum” do
require ‘rubygems’; require ‘ruby-debug’; debugger
5.days.to_i.should == 5.days
end

Scott

On 1 Jul 2008, at 13:06, Matt D. wrote:

When run via ‘rake spec:models’
Date.today = 2008-07-01
self.start_date = 0825-09-17
self.end_date = 2008-07-02

Pretty obvious what the bug is suddenly, but would cause this?

Not a solution, but a clue:

$ ./script/console

Date.today.minus_with_duration(5.days)
=> Thu, 26 Jun 2008

Date.today.minus_without_duration(5.days.to_i)
=> Sun, 17 Sep 0825

Is this a load-order problem? Perhaps
ActiveSupport::CoreExtensions::date::Calculations isn’t kicking in
correctly.

Cheers,
-Tom

Yeah - use the debugger (ruby-debug) and the failing test case:

it “should have 5.days as the proper fixnum” do
require ‘rubygems’; require ‘ruby-debug’; debugger
5.days.to_i.should == 5.days
end

Thanks for the reply, but what does this resolve?
It seems as this would be a bug upsteam, no?

On Jun 30, 2008, at 9:43 PM, Matt D. wrote:

On Jul 1, 2008, at 10:10 PM, Camilo T. wrote:

It seems your Phase.percentage_complete is not working as you expect.
It clearly returns 100 when you are specting 83. The test clearly
said
that.

¿What is the implementation of your percentage_complete method?

It only fails when run via ‘rake spec:models’; otherwise, it passes.

Just catching up with this. When you say ‘otherwise’, how else are you
running this that it passes?

Can you try these and tell us which ones pass and which ones fail?

rake spec
rake spec:models
script/spec spec
script/spec spec/models
script/spec spec/models -r
script/spec spec/models/phase_spec.rb
script/spec spec/models/phase_spec.rb -r
ruby spec/models/phase_spec.rb
ruby spec/models/phase_spec.rb -r

Thanks,
David

ruby spec/models/phase_spec.rb -r

Thanks,
David

Ahhh, Mr. Chelimsky himself :wink:

rake spec fails with:
Date.today = 2008-07-03
self.start_date = 0825-09-19
self.end_date = 2008-07-04

rake spec:models fails with:
Date.today = 2008-07-03
self.start_date = 0825-09-19
self.end_date = 2008-07-04

script/spec spec passes with:
Date.today = 2008-07-03
self.start_date = 2008-06-28
self.end_date = 2008-07-04

script/spec spec/models passes with:
Date.today = 2008-07-03
self.start_date = 2008-06-28
self.end_date = 2008-07-04

script/spec spec/models -r fails altogether with:
optparse.rb:451:in `parse’: missing argument: -r
(OptionParser::MissingArgument)

script/spec spec/models/phase_spec.rb passes with:
Date.today = 2008-07-03
self.start_date = 2008-06-28
self.end_date = 2008-07-04

script/spec spec/models/phase_spec.rb -r fails with the
MissingArgument exception

ruby spec/models/phase_spec.rb passes with:
Date.today = 2008-07-03
self.start_date = 2008-06-28
self.end_date = 2008-07-04

ruby spec/models/phase_spec.rb -r fails with the MissingArgument
exception