[rspec-users] Stubbing errors for AR

Jerry West jerry.west at ntlworld.com
Sun Apr 29 20:01:11 EDT 2007


I'm not at my desk, and can't remember seeing #add_stubs before, though 
I can guess what it does.  However, you seem to be using the parameter m 
yielded by mock_model as both the class (model) and the instance.  If m 
is the class then the #should_receive(:find) will work as expected.  But 
if so, does that mean #add_stubs is putting the stub for save on Setting 
(the class) and not @setting?

I would make the expectation explicit: 
@setting.should_receive(:save).and_return(false).  If that works check 
to see what mock_model is yielding (class or instance) and that 
add_stubs does what you think it does.

If the expectation on @setting doesn't make any difference, then I'm 
stumped without spending much more time on this (it's nearly 1am here, I 
*really* should not be doing this!).

Best wishes,

s.ross wrote:
> 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

More information about the rspec-users mailing list