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