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

Bryan Ray bryansray at gmail.com
Mon Mar 10 19:38:02 EDT 2008


Try this:

> before(:each) do
>     @review = mock_model(Review, :id => 1 )
>     @description = mock(Description,  :id => 1,
>                                       :review_id => 1 )
>
> 	# .descriptions should return an "Array of descriptions" ... so  
> let's make that here
> 	@descriptions = [@description]
>
> 	# This will stub out the 'descriptions' call .. after setting it  
> here, I typically find it wise to right a it "should happen"  
> statement below
> 	# This means whenever "descriptions" is called in your "show"  
> method return @descriptions
>     	@review.stubs!(:descriptions).and_returns(@descriptions)
> end

This would be another test that 'should' happen ... (read up on stubs  
vs. mocks - there is tons of information on google)

> it "should have a list of descriptions" do
> 	@review.should_receive(:descriptions).and_return(@descriptions)
> 	do_get
> end

I'm not entirely sure why you're doing creating a new  
Desription.kaizen() ... it seems to me like you would have an  
association in the Review model that would allow that to be called,  
but that's neither here nor there.

The code above should get your spec working ...

Hope this helps a little. Good luck, buddy.

On Mar 10, 2008, at 5:33 PM, Bryan Wheelock wrote:

> 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 
>  _______________________________________________
> rspec-devel mailing list
> rspec-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-devel/attachments/20080310/b15ca455/attachment-0001.html 


More information about the rspec-devel mailing list