[rspec-users] Date Approximation in Specs

s.ross cwdinfo at gmail.com
Wed Jan 10 14:01:57 EST 2007


I upgraded to trunk and changed the code as follows:

module DateExpectations
     HOUR_TOLERANCE = 60 * 60
     DAY_TOLERANCE = HOUR_TOLERANCE * 24
     WEEK_TOLERANCE = DAY_TOLERANCE * 7

   class BeCloseTo
     def initialize(target_date, tolerance)
       @target_date = Time.parse(target_date)
       @tolerance = tolerance
     end

     def met_by?(target)
       compare_date = target - @target_date
       adjustment = @tolerance.inject do |item|
           case item
           when :days
               adjustment += DAY_TOLERANCE
           when :weeks
               adjustment += WEEK_TOLERANCE
           end
       end
       return true if compare_date.abs < adjustment
     end

     def failure_message
       return "#{@target_date} does not fall within tolerance."  # s/ 
b better
     end
   end

   def should_be_close_to(target_date, tolerance)
     return BeCloseTo.new(target_date, tolerance)
   end
end

context "My package" do
   include DateExpectations
   specify "should be delivered no later than 2 days after my order" do
     1.day.ago.should_be_close_to("1/9/2007", :days => 2)   # <- line 40
   end
end

The result is:

1)
NoMethodError in 'My package should be delivered no later than 2 days  
after my order'
undefined method `close_to?' for Tue Jan 09 10:59:31 -0800 2007:Time
./spec/helpers/date_range_spec.rb:40:


Any thoughts as to why? (Note: You had written the expectation in  
your sample as:
"should<space>be_close_to" -- was that intentional?).

Steve


On Jan 9, 2007, at 8:26 PM, David Chelimsky wrote:

> module DateExpectations
>   class BeCloseTo
>     def initialize(target_date, tolerance)
>       @target_date = target_date
>       @tolerance = tolerance
>     end
>
>     def met_by?(target)
>       #return true if target is a date
>       #that meets the expectation
>     end
>
>     def failure_message
>       #return an appropriate failure message
>     end
>   end
>
>   def should_be_close_to(target_date, tolerance)
>     return BeCloseTo.new(target_date, tolerance)
>   end
> end
>
> context "My package" do
>   include DateExpectations
>   specify "should be delivered no later than 2 days after my order" do
>     delivery_date.should be_close_to("1/9/2007", :days => 2)
>   end
> end



More information about the rspec-users mailing list