[rspec-users] Stub activerecord find given instance?

Saverio Miroddi lists at ruby-forum.com
Fri Feb 5 09:10:55 EST 2010


Thanks for the help - I'm going to look directly at the source code,
though of course the recommendation of improving the tests rather than
stretch the api itself is very welcome!

If I'm going to find anything notable (I have kind of a hunch that I
will), I'll post it.

Saverio

Paul Hinze wrote:
> On Sun, Jan 24, 2010 at 5:10 PM, Saverio Miroddi <lists at ruby-forum.com> 
> wrote:
>> class MyController < ...
>> �def show
>> � �m = MyModel.find( params[ :id ] )
>> � �mp = m.my_parent
>>
>> � �@m_value = m.value
>> � �@mp_value = mp.value
>> �end
>> end
> 
> Hi Saverio,
> 
> Following this thread, and still having trouble understanding what it
> it you are trying to accomplish.
> 
>> now, suppose I want to stub a behavior only for the child object ('m').
>> so my intention in the spec is to:
>>
>> - create the child and his parent
>> - stub MyModel#value in the child
>> - stub MyModel.find to return "m" _only_ when called with the child id,
>> otherwise it should do its usual business. if I stub generically,
>> m#my_parent would return "m" itself.
> 
> The below test does not accomplish this third point...  I'll explain 
> below.
> 
>> the test would be:
>>
>> it "should display a stubbed value for the children" do
>> �mp = MyModel.create!( :value => 0xCAFEBABE )
>> �m �= MyModel.create!( :my_parent_id => mp.id, :value => 64738 )
>>
>> �m.stub!( :value ).with( m.id ).and_return( 42 )
> 
> Here you are creating a stub on the instance `m`, not stubbing
> `MyModel.find` as you explain above.  Perhaps you meant something more
> like this?
> 
>   m.stub!(:value).and_return(42)
>   MyModel.stub!(:find).with(m.id).and_return(m)
> 
-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list