On Mar 29, 2008, at 4:45 PM, elmo wrote:
Here's where I get stuck. I want to create an expectation for the
find() method that I call on the ActiveRecord result set.
I want to set an expectation for this line of code:
@comments = @post.comments.find(:conditions => ['user_id = ? ',current_user ] )
My thought was that I could stub the find() method to the Comment class:
Comment.stub!(:find).and_return( [@comments] )
But, this does not work, so I'm clearly missing something. I'd appreciate any
insight that you'd be willing to share.
I think what you're asking for is this:
@comment = mock_model(Comment)
@comments = [@comment]
comments_proxy = mock(Array)
# Make sure the post receives comments
# Assuming there is a current user mock available.
comments_proxy.should_receive(:find).with(:conditions => ['user_id = ?', @current_user]).and_return(@comments)
Then it will tell you if it those objects never received those methods.