[rspec-users] More on collection proxies

Jay Levitt lists-rspec at shopwatch.org
Sun Jan 21 18:09:18 EST 2007


David Chelimsky wrote:
> On 1/21/07, Jay Levitt <lists-rspec at shopwatch.org> wrote:
>> 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:

[snip]

>> 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?
> 
> The next question is, why are we passing in the date? We want to how
> many days have passed since it was completed, not how many days
> between completion and some arbitrary date. 

Actually, we want the latter - but of course you couldn't tell that from 
my method name!  The tasks are repeating, and this app fills in a 
spreadsheet-like series of cells, one per day, with various colors, 
depending on how often you're completing a task.  So we actually need to 
check the task's status on any given day.

[..]
>     event_stub = Struct.new(:date,
> :status).new(Time.parse("2001-02-03"), "completed")
>     @task.stub!(:events).and_return([event_stub])
[..]

You don't mind stubbing the association proxy as a struct?  That was my 
real question; it seems to lead down a messy road of trying to stub 
every ActiveRecord method.  On the one hand, it seems like BDD says I 
should stub or mock any object other than the one under test.  On the 
other, since I'm still in the models realm, maybe it's okay to use 
fixtures to represent the model relations.  Or maybe there's a third way.

Jay



More information about the rspec-users mailing list