[rspec-users] Date Approximation in Specs

s.ross cwdinfo at gmail.com
Thu Jan 11 02:18:11 EST 2007


My net connection's gone spotty over the last couple of days, but I  
wanted to follow up and tell you that your new code works great.  
Here's just a proof of concept implementation (complete with hard  
coding!):

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
       compare_span = target - @target_date
       adjustment = 0
       @tolerance.each do |key, amount|
           case key
           when :hours
               adjustment += HOUR_TOLERANCE * amount
           when :days
               adjustment += DAY_TOLERANCE * amount
           when :weeks
               adjustment += WEEK_TOLERANCE * amount
           end
       end
       return true if compare_span.abs < adjustment
     end

     def failure_message
       return "#{@target_date} does not fall within tolerance (# 
{@tolerance.collect{|k,v| "#{k.to_s} => #{v}"}.join(", ")}) from # 
{@compare_date}."
     end
   end

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

context "The DateRange helper" do
   include DateExpectations
   specify "should be able to approximate within two days" do
     1.day.ago.should be_close_to("1/9/2007", :days => 2)
   end
end


On Jan 10, 2007, at 11:22 AM, David Chelimsky wrote:

> On 1/10/07, s.ross <cwdinfo at gmail.com> wrote:
>> 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?).
>
> Yes!!!! That's the whole point. ;)
>
> should be_close_to
>
> and your method should be named be_close_to
>
> Let me know how it works out.
>
> Thanks for giving this a shot - you're the guinea pig!
>
> David
>
>>
>> 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
>>
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
>>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list