<br><br><div><span class="gmail_quote">On 8/24/07, <b class="gmail_sendername">Courtenay</b> &lt;<a href="mailto:court3nay@gmail.com">court3nay@gmail.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Does this work?<br><br>&nbsp;&nbsp; Image.stub!(:validates_uniqueness_of).and_return(true)<br><br><br>On 8/23/07, s.ross &lt;<a href="mailto:cwdinfo@gmail.com">cwdinfo@gmail.com</a>&gt; wrote:<br>&gt; I want to use mocks and stubs to test the controller, but am having
<br>&gt; trouble getting my validation not to trigger. Here&#39;s the code:<br>&gt;<br>&gt; # spec:<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image.stub!(:find).and_return(@image)<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@image.should_receive(:save!).once.with(:any_args)
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;put :update, :id =&gt; @<a href="http://image.id">image.id</a>, :category_id =&gt;<br>&gt; @<a href="http://category.id">category.id</a>, :image =&gt; {:name =&gt; &#39;test&#39;, :image_number =&gt; 13554}<br>
&gt;<br>&gt; #model<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;validates_presence_of :name<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;validates_uniqueness_of :name, :allow_nil =&gt; true<br>&gt;<br>&gt; # rspec output<br>&gt;<br>&gt; ActiveRecord::RecordInvalid in &#39;ImagesController should update a
<br>&gt; database record when it receives a PUT&#39;<br>&gt; Validation failed: Name has already been taken, Image number has<br>&gt; already been taken<br>&gt;<br>&gt;<br>&gt; It seems AR is not detecting that this is an edit/update that will
<br>&gt; not cause a uniqueness conflict. I believe the code in the model<br>&gt; needs to remain in place because I don&#39;t want a user creating a name<br>&gt; conflict by editing an existing name into one that already exists in
<br>&gt; the database. Any thoughts on how better to spec this?<br>&gt;<br>&gt; Thanks<br>&gt; _______________________________________________</blockquote><div><br>
Why use a real image at all?&nbsp; I usually use mock_model on these
and then stub/mock the specific calls in the controller method.&nbsp;
This way your not testing the model at all.&nbsp; Just the
controller.&nbsp; Of course, they can get pretty complex with all the
stubbing etc.<br>
<br>
 &nbsp; &nbsp; Image.stub!(:find).and_return(@image)<br>
 &nbsp; &nbsp; @image.should_receive(:save!).once.with(:any_args)<br>
 &nbsp; &nbsp; put :update, :id =&gt; @<a onclick="return top.js.OpenExtLink(window,event,this)" href="http://image.id" target="_blank">image.id</a>, :category_id =&gt;<br>
@<a onclick="return top.js.OpenExtLink(window,event,this)" href="http://category.id" target="_blank">category.id</a>, :image =&gt; {:name =&gt; &#39;test&#39;, :image_number =&gt; 13554}<br>
<br>
could become.<br>
<br>
@category = mock_model( Category, :id =&gt; 1 ) # Not sure where this one is used other than the call to put<br>
@image = mock_model( Image, :id =&gt; 2 )<br>
@image.should_receive( :save! ).once.with( :any_args ).and_return( true )<br>
Image.stub!( :find ).and_return( @image )<br>
<br>
 put :update, :id =&gt; @<a onclick="return top.js.OpenExtLink(window,event,this)" href="http://image.id" target="_blank">image.id</a>, :category_id =&gt;<br>

@<a onclick="return top.js.OpenExtLink(window,event,this)" href="http://category.id" target="_blank">category.id</a>, :image =&gt; {:name =&gt; &#39;test&#39;, :image_number =&gt; 13554}<br>
<br>
This way you&#39;re not reaching into the model to check your controller.<br>
<br>
HTH<br>
Daniel<br>
</div><br></div><br>