[rspec-users] Date Approximation in Specs

David Chelimsky dchelimsky at gmail.com
Tue Jan 9 23:26:42 EST 2007


On 1/9/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> On 1/9/07, s.ross <cwdinfo at gmail.com> wrote:
> > The floating-point expectations allow for an error tolerance. Is
> > there any similar facility for dates? For example, say I have a
> > custom class that handles date/time spans and I want to spec it:
> >
> > context "A DateRange span" do
> >    specify "should know when a week ago is :)" do
> >      d = DateRange.new
> >      d.last_week.should_be_close_to(1.week.ago, 24*60*60)
> >    end
> > end
> >
> > The idea of should_be_close_to is to provide a tolerance. My class
> > only is required to be accurate to the same day, and I want to
> > express that in terms of "close_to" uncertainty. Is there a great
> > baked-in way I'm missing?
> >
> > I have code to do this in Test::Unit that I could port over if need
> > be but I'd rather not if something better already exists.
>
> Doesn't exist yet. Feel free to add a feature request:
>
> http://rubyforge.org/tracker/?group_id=797

Hey Steve - I just committed a small enhancement to rspec core that is
intended to pave the way for custom expectations. Are you working w/
0.7.5 or the trunk? If you're working w/ trunk, grab the latest trunk
and do this:

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

This is only experimental today, but will very likely become the entry
way for custom expectations. Let me know how it works out for you.

Cheers,
David

>
> > Thanks,
> >
> > Steve
> > _______________________________________________
> > rspec-users mailing list
> > rspec-users at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/rspec-users
> >
>


More information about the rspec-users mailing list