On Mon, Jul 7, 2008 at 2:28 AM, Sam Granieri, Jr &lt;<a href="mailto:sam@samgranieri.com">sam@samgranieri.com</a>&gt; wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I&#39;m working on some code that uses association proxies. I&#39;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.<br>

<br>
I&#39;m on edge rails and edge rspec.<br>
<br>
Datawise, a bar has_many suggested_beers, and a suggested_beer belongs to a bar<br>
<br>
suggested_beers_controller<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
class SuggestedBeersController &lt; ApplicationController<br>
 &nbsp;before_filter :find_bar<br>
<br>
 &nbsp;#code omitted for brevity<br>
 &nbsp;# GET /bars/1/suggested_beers/new<br>
 &nbsp;def new<br>
 &nbsp; &nbsp;@suggested_beer = @bar.suggested_beers.build<br>
<br>
 &nbsp; &nbsp;respond_to do |format|<br>
 &nbsp; &nbsp; &nbsp;format.html<br>
 &nbsp; &nbsp;end<br>
 &nbsp;end<br>
<br>
 &nbsp; # POST /bars/1/suggested_beers<br>
 &nbsp;def create<br>
 &nbsp; &nbsp;@suggested_beer = @bar.suggested_beers.build(params[:suggested_beer])<br>
 &nbsp; &nbsp;#more redaction<br>
 &nbsp;end<br>
<br>
 &nbsp;#code omitted for brevity<br>
 &nbsp;private<br>
 &nbsp;def find_bar<br>
 &nbsp; &nbsp;@bar &nbsp;= Bar.find(params[:bar_id])<br>
 &nbsp;end<br>
</blockquote>
<br>
<br>
<br>
suggested_beers_controller_spec.rb<br>
<br>
This is generated with rspec_scaffold, and i&#39;ve added in the beer_id, bar_id, and ip_address attributes for stubbing.<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
&nbsp;def mock_suggested_beer(stubs={})<br>
 &nbsp; &nbsp;stubs = {<br>
 &nbsp; &nbsp; &nbsp;:save &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=&gt; true,<br>
 &nbsp; &nbsp; &nbsp;:update_attributes =&gt; true,<br>
 &nbsp; &nbsp; &nbsp;:destroy &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =&gt; true,<br>
 &nbsp; &nbsp; &nbsp;:to_xml &nbsp; &nbsp; =&gt; &#39;&#39;,<br>
 &nbsp; &nbsp; &nbsp;:beer_id &nbsp; &nbsp;=&gt; &quot;1&quot;,<br>
 &nbsp; &nbsp; &nbsp;:bar_id &nbsp; &nbsp; =&gt; &quot;1&quot;,<br>
 &nbsp; &nbsp; &nbsp;:ip_address =&gt; &quot;<a href="http://127.0.0.1" target="_blank">127.0.0.1</a>&quot;<br>
 &nbsp; &nbsp;}.merge(stubs)<br>
 &nbsp; &nbsp;@mock_suggested_beer ||= mock_model(SuggestedBeer, stubs)<br>
 &nbsp;end<br>
<br>
 &nbsp;describe &quot;responding to GET /bars/1/suggested_beers/new&quot; do<br>
<br>
 &nbsp; &nbsp;it &quot;should succeed&quot; do<br>
 &nbsp; &nbsp; &nbsp;get :new, :bar_id =&gt; &quot;1&quot;<br>
 &nbsp; &nbsp; &nbsp;response.should be_success<br>
 &nbsp; &nbsp;end<br>
<br>
 &nbsp; &nbsp;it &quot;should render the &#39;new&#39; template&quot; do<br>
 &nbsp; &nbsp; &nbsp;get :new, :bar_id =&gt; &quot;1&quot;<br>
 &nbsp; &nbsp; &nbsp;response.should render_template(&#39;new&#39;)<br>
 &nbsp; &nbsp;end<br>
<br>
 &nbsp; &nbsp;it &quot;should create a new suggested_beer&quot; do<br>
 &nbsp; &nbsp; &nbsp;SuggestedBeer.should_receive(:build).with().and_return(mock_suggested_beer)<br>
 &nbsp; &nbsp; &nbsp;get :new, :bar_id =&gt; &quot;1&quot;<br>
 &nbsp; &nbsp;end<br>
</blockquote>
&quot;should create a new suggested_beer&quot; fails with this message<br>
<br>
Spec::Mocks::MockExpectationError in &#39;SuggestedBeersController responding to GET /bars/1/suggested_beers/new should create a new suggested_beer&#39;<br>
Mock &#39;SuggestedBeer_1016&#39; received unexpected message :[]= with (&quot;bar_id&quot;, 1)<br>
</blockquote><div><br>The way I approach this is to stub the object which has the association proxy to return a mock for the proxy.&nbsp; To do that you have to stub whatever method your controller uses to get that object.<br>
<br>In this case, assuming that your controller is setting up @bar in the obvious way, it would be something like. <br><br>it &quot;should create a new suggested_beer&quot; do<br>&nbsp;&nbsp;&nbsp;&nbsp; suggested_beers = mock(&quot;suggested_beers&quot;)<br>
&nbsp;&nbsp;&nbsp;&nbsp; bar = mock_model(Bar, :suggested_beers =&gt; suggested_beers)<br>&nbsp;&nbsp;&nbsp;&nbsp; Bar.stub!(:find).and_return(bar)<br>&nbsp;&nbsp;&nbsp;&nbsp; suggested_beer = mock_model(SuggestedBeer, ....)<br>
 &nbsp; &nbsp; &nbsp;SuggestedBeer.should_receive(:build).with(:blatz)..and_return(mock_suggested_beer)<br>&nbsp; &nbsp;&nbsp;
get :new, :bar_id =&gt; &quot;1&quot;, :suggested_beer =&gt; :blatz<br>&nbsp;end<br></div></div><br><br>Some would break this down into at least two examples, one just checking that the build method is called and another checking that it is called with the expected paramenters.<br>
<br>-- <br>Rick DeNatale<br><br>My blog on Ruby<br><a href="http://talklikeaduck.denhaven2.com/">http://talklikeaduck.denhaven2.com/</a>