[rspec-users] Stubbing errors for AR
s.ross
cwdinfo at gmail.com
Sun Apr 29 20:04:42 EDT 2007
Here's what I wound up doing:
setup do
@countable = mock('countable')
@countable.stub!(:count).and_return(1)
@countable.stub!(:full_messages).and_return(['a message'])
@setting = mock_model Setting do |m|
m.stub!(:save).and_return(true)
m.stub!(:errors).and_return(@countable)
m.stub!(:setting_name).and_return('first_name')
m.stub!(:setting_value).and_return('first_value')
m.stub!(:setting_type).and_return('first_type')
end
end
That solves the original problem of the view requesting error
messages and barfing and also cleans up the code a lot.
Thanks!
On Apr 29, 2007, at 5:01 PM, Jerry West wrote:
> Steve,
>
> 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,
> Jerry
>
>
> 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
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
More information about the rspec-users
mailing list