[rspec-devel] How to deal with has_many associations?

Bryan Wheelock bryan.wheelock at gmail.com
Mon Mar 10 18:33:16 EDT 2008


I'm new to rSpec and I've been having a hard time working with has_many
associations and mocks.

Review has_many :descriptions

I have a Reviews_controller::show method:

def show
  @review = Review.find(params[:id])
  @review.descriptions << Description.kaizen(@review.id)
# Description.kaizen returns a Description object

  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @review }
  end
end

I've been trying to create a spec to test this association:

describe "handling GET /reviews/1" do


  before(:each) do
    @review = mock_model(Review, :id => 1 )
    @description = mock(Description,  :id => 1,
                                      :review_id => 1 )
   # this is all added in an attempt to make the has_many association work

    description_proxy = mock('association proxy')
    @review.stubs(:descriptions).and_returns(description_proxy)
    description_proxy.expects(:<<).with(@description)
  end

  def do_get
    get :show, :id => "1"
  end

  it "should be successful" do
    do_get
    response.should be_success
  end
end

When I run the spec, I get the following error:
Mock 'Review_1007' received unexpected message :stubs with (:descriptions)

I can't seem to find any examples via Google.

Why is my has_many association not being recognized?
What is the proper rSpec way to handle this?

thanks,
Bryan



-- 
The best marketing related articles are at
http://www.InstantDirectMarketing.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-devel/attachments/20080310/0c6571a4/attachment.html 


More information about the rspec-devel mailing list