[rspec-users] Mocking Rails association collections

David Chelimsky dchelimsky at gmail.com
Wed Jul 18 17:30:08 EDT 2007


On 7/18/07, Paul <psadauskas at absolute-performance.com> wrote:
> Rails model association collections allow you to do nifty things like:
>
>   article.comments.find(:all, :conditions => {:created_at > 1.day.ago})
>
> Has anyone found a good way to mock this up? I'm currently doing this:
>
>   @comment1 = mock_model(Comment)
>   comments = mock(Array)
>   comments.stub!(:find).and_return([@comment1])
>
>   @article = mock_model(Article)
>   @article.stub!(:comments).and_return(comments)
>
> I don't like this, because of that intermediate 'comments' object, whose
> only purpose is so that i can stub the chained method. I'd like to do
> something like this:
>
>   @comment1 = mock_model(Comment)
>
>   @article = mock_model(Article, :comments => mock(Array, :find =>
> [@comment1]))
>
> But trying this causes an error: "Mock 'Array' received unexpected
> message :find with (:all, ...)" because you can't inline stubs with
> ordinary `mock`. I can replace it with `mock_model`, but this feels unclean.
>
> Has anyone come across a good 'best-practice' solution to this problem?

You can use the stub() method instead of mock() to inline method stubs:

@article = mock_model(
  Article, :comments => stub(Array, :find => [@comment1])
)

The mock() method works differently because it does different stuff w/
the Hash under the covers.

>
> TIA,
> Paul Sadauskas
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list