[rspec-users] More on collection proxies

Jay Levitt lists-rspec at shopwatch.org
Sun Jan 21 15:46:23 EST 2007


I've got a question similar to Francois's.  I'm writing a to-do-like 
application, where each Task has a number of Events, consisting of 
event.date and event.status.  I want to see how long it's been since the 
Task was last completed.  So:

context "A task completed once on 2001-01-02" do
   setup do
     @task = Task.new
     mock_event = Struct.new(:date, :status)
     @task.stub!(:events).and_return(
       [mock_event.new(Date.parse("2001-01-02"), "completed")])
   end

   specify "was completed 0 days ago on 2001-01-02" do
     @task.completed_days_ago(Date.parse("2001-01-02")).should_eql 0
   end
end

task.rb looks like:

   def cache_completions
     @completions = {}
     events.each do |event|
       @completions[event.date] = event.status
     end
   end

   def completed_days_ago(todays_date)
     cache_completions unless @completions
     date = todays_date
     while date > todays_date - 7 do
       return (todays_date - date).to_i if @completions[date] == "completed"
       date = date - 1
     end

     "never"
   end


This smells a bit.  I could separate part of completed_days_ago into 
last_completed_on(date), but no matter how I slice it I still end up 
mocking Task.cells for some routine.  Suggestions?

Jay




More information about the rspec-users mailing list