[rspec-users] Trouble with Association Proxies and new/create in rails

Sam Granieri, Jr sam at samgranieri.com
Mon Jul 7 02:28:21 EDT 2008


I'm working on some code that uses association proxies. I'm trying to  
get away from using fixtures, and move more to mocking and stubbing.  
So far, so good, but the methods I cant figure out, even with  
extensive googling, are how to simulate the association_proxy.build  
method in rails.

I'm on edge rails and edge rspec.

Datawise, a bar has_many suggested_beers, and a suggested_beer belongs  
to a bar

suggested_beers_controller

> class SuggestedBeersController < ApplicationController
>   before_filter :find_bar
>
>   #code omitted for brevity
>   # GET /bars/1/suggested_beers/new
>   def new
>     @suggested_beer = @bar.suggested_beers.build
>
>     respond_to do |format|
>       format.html
>     end
>   end
>
>    # POST /bars/1/suggested_beers
>   def create
>     @suggested_beer =  
> @bar.suggested_beers.build(params[:suggested_beer])
>     #more redaction
>   end
>
>   #code omitted for brevity
>   private
>   def find_bar
>     @bar  = Bar.find(params[:bar_id])
>   end



suggested_beers_controller_spec.rb

This is generated with rspec_scaffold, and i've added in the beer_id,  
bar_id, and ip_address attributes for stubbing.

>  def mock_suggested_beer(stubs={})
>     stubs = {
>       :save              => true,
>       :update_attributes => true,
>       :destroy           => true,
>       :to_xml     => '',
>       :beer_id    => "1",
>       :bar_id     => "1",
>       :ip_address => "127.0.0.1"
>     }.merge(stubs)
>     @mock_suggested_beer ||= mock_model(SuggestedBeer, stubs)
>   end
>
>   describe "responding to GET /bars/1/suggested_beers/new" do
>
>     it "should succeed" do
>       get :new, :bar_id => "1"
>       response.should be_success
>     end
>
>     it "should render the 'new' template" do
>       get :new, :bar_id => "1"
>       response.should render_template('new')
>     end
>
>     it "should create a new suggested_beer" do
>        
> SuggestedBeer.should_receive(:new).and_return(mock_suggested_beer)
>       get :new, :bar_id => "1"
>     end
"should create a new suggested_beer" fails with this message

Spec::Mocks::MockExpectationError in 'SuggestedBeersController  
responding to GET /bars/1/suggested_beers/new should create a new  
suggested_beer'
Mock 'SuggestedBeer_1016' received unexpected message :[]= with  
("bar_id", 1)


I don't know how to fix this.

I've looked at several articles, specifically
http://www.lukeredpath.co.uk/2007/10/18/demeters-revenge
and http://www.ruby-forum.com/topic/158489
but I'm still confused.

Any help is much appreciated!



--------------------------------
Sam Granieri, Jr
sam at samgranieri.com
Chicago, IL

irc (freenode):    	samgranieri

http://www.samgranieri.com
http://www.beerbin.com

Recommend me at WWR!
http://www.workingwithrails.com/person/7374-sam-granieri



More information about the rspec-users mailing list