addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscontroller-playcrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobegmailgooglegroupshelp-with-circleimageimagesinstagramFill 1light-bulblinklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonprintShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruserwarningyahoo

Re: [ruby-81] I could use a little help with some BDD / rpspec concepts.

From: William S.
Sent on: Saturday, March 29, 2008 11:00 PM

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
@post.should_receive(:comments).and_return(comments_proxy)

# 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.

Cheers,
- Will

Our Sponsors

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy