[rspec-users] Stubbing errors for AR

s.ross cwdinfo at gmail.com
Sun Apr 29 18:13:01 EDT 2007


Absolutely and totally helpful. What I'm missing is the je ne sais  
quois about how mock_model works. I'd be happy to submit a doc patch  
with an example if I could just get one :)

Here's what I'm doing and tell me if my approach is haywire or I'm  
misunderstanding:

# the spec

it "should fail with POST and bad data"
   mock_model Setting do |m|      # seems mock_model now takes a  
classname and not a symbol
     m.should_receive(:find).once.and_return(@setting)
     add_stubs(m, :save => false) # this can go into the second param  
to mock_model, I guess
   end

   puts "for debugging purposes, save should result in false"
   POST :update, :id => 1, :setting => {:setting_name =>  
'first_new_setting', :setting_value =>  
'first_new_value', :setting_type => 'string'}
   response_should be_success # because success renders action 'edit'  
and sets a 200
end

# the controller

def update
   @setting = Setting.find(params[:id])
   puts "setting is #{@setting.save}"
   # ...
end


What I'm seeing from the puts statements is:

for debugging purposes, save should result in false
setting is true


What am I missing about how this should work?

Thanks

Steve


On Apr 29, 2007, at 2:15 AM, Jerry West wrote:

> Look for mock_model (in the plugin examples???) and/or google for
> 'making a mockery of ActiveRecord'.
>
> Hope this helps,
>    Jerry
>
> s.ross wrote:
>> When mocking for an update method, I'm using this code.
>>
>>      @mock_setting = mock("setting")
>>      Setting.should_receive(:find).once.and_return(@mock_setting)
>>      @mock_setting.stub!(:save).and_return(false)
>>
>> All well and good, as it's supposed to generate a failed update.
>> However, the
>>
>> <%= error_messages_for 'setting' %>
>>
>> wants a receiver for errors. Has anyone stubbed errors? The AR errors
>> object has to respond to a bunch of stuff and I wanted to find out if
>> someone had already done this (or if it's a dumb idea).
>>
>> Thanks,
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list