align-toparrow-leftarrow-rightbackbellblockcalendarcamerachatcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-crosscrosseditfacebookglobegoogleimagesinstagramlocation-pinmagnifying-glassmailmoremuplabelShape 3 + Rectangle 1outlookpersonplusImported LayersImported LayersImported Layersshieldstartwitteryahoo

I could use a little help with some BDD / rpspec concepts.

From: Elliott B.
Sent on: Saturday, March 29, 2008 4:45 PM
Hello everyone at meetup,
I'm back in Boston for a while, and I've been working on getting my BDD chops in order.?
Before I get into my main question, I've got some general questions:
If you consider yourself skilled with BDD/rspec, how did you become so skilled?
What particular methodology did you use to study this stuff?
(I've watched three of the peepcode movies, and read the BDD chapter in 'The Rails Way')
Are there better learning resources that you could recommend?
 if you are using BDD on a regular basis, do you find it worth the effort?
I guess my comments imply a certain level of frustration, but I want to like BDD more than I currently do.

.....Anyway, here's what I'm trying to understand:

 Let's say I have a nested (has_many) relationship using an example we are all familiar with.
class Post
  has_many :comments
class Comment
  belongs_to  :post
Now, assuming the RESTful pattern, I have a before filter to get the
current_user and to pick up the nesting @post.

class CommentsController
   before_filter  :login_required  #set current_user
   before_filter  :get_post        #set @post
  [... the usual rest scaffold stuff ...]
  def index
    @comments = @post.comments.find(:conditions => ['user_id = ? ',current_user ] )
    def get_post
      @post = Post.find(params[:post_id])

#The Problem
Because of the ActiveRecord association,  @post receives the method comments()
I can express this rspec expectation like this:
  @post = posts(:posts_001)  #a mock post object from fixtures
  @comment = comments(:comment_001) # a mock comment from fixtures
  @post.stub!(:comments).and_return([@comment])  #stub @post.comments to return array of containing one comment. far, so  good.
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.

Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.

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