Stub activerecord find given instance?

Is there a clean/simple way of stubbing the activerecord find() for a
single instance?

As usual pattern, I see something like

myModel = MyModel.new(…)
MyModel.stub( :find ).and_return( myModel )

which can give problems in case we want to find other instances.

???
Saverio

On 10 Nov 2009, at 14:08, Saverio M. wrote:

Is there a clean/simple way of stubbing the activerecord find() for a
single instance?

MyModel.stub(:find).with(42).and_return(myModel)

Tom S. wrote:

On 10 Nov 2009, at 14:08, Saverio M. wrote:

Is there a clean/simple way of stubbing the activerecord find() for a
single instance?

MyModel.stub(:find).with(42).and_return(myModel)

Didn’t work as expected - I’ll do a bit of research and post again.

On Sat, Dec 12, 2009 at 1:35 PM, Saverio M. [email protected]
wrote:

Tom S. wrote:

On 10 Nov 2009, at 14:08, Saverio M. wrote:

Is there a clean/simple way of stubbing the activerecord find() for a
single instance?

MyModel.stub(:find).with(42).and_return(myModel)

Didn’t work as expected - I’ll do a bit of research and post again.

What is expected? What are you trying to accomplish?

On Tue, Jan 12, 2010 at 6:58 PM, Saverio M.
[email protected]wrote:

class MyController < ApplicationController
end

get :show, :code => “kkk”
end
end

##################################################

I’m expecting nothing to be printed, instead “triggered ‘abc’!” is
printed.

That’s actually the expected behaviour. When you pass a block to a call
to
stub, with, or and_return, the block is evaluated and the resulting
value is
returned to the caller. So in the show method, when
MyModel.find_by_code(
params[:code] ), the block in the example is invoked.

It’d be much easier to help you if you could provide an example more
representative of what you are actually trying to accomplish.

David C. wrote:

MyModel.stub(:find).with(42).and_return(myModel)
Didn’t work as expected - I’ll do a bit of research and post again.
What is expected? What are you trying to accomplish?

Example in horror-code:

##################################################

class MyController < ApplicationController
def show
MyModel.find_by_code( params[:code] )
head :ok
end
end

describe “MySuite” do
it “should” do
MyModel.stub!( :find_by_code ).with( “abc” ) do
puts “triggered ‘abc’!”
end

get :show, :code => "kkk"

end
end

##################################################

I’m expecting nothing to be printed, instead “triggered ‘abc’!” is
printed.

The example is, as I said, completely meaningless, but would simplify
some tests in our suites.

David C. wrote:

It’d be much easier to help you if you could provide an example more
representative of what you are actually trying to accomplish.

So, here it is (note: this is NOT representative of a properly
engineered behavior):

class MyModel
def my_parent
self.find( my_parent_id )
end
end

class MyController < …
def show
m = MyModel.find( params[ :id ] )
mp = m.my_parent

@m_value = m.value
@mp_value = mp.value

end
end

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 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 )

get :show, :id => m.id
end

what happens is:

  • if I don’t use “.with( m.id )”, @m_value and @mp_value will have
    m.value assigned.
  • if I use “.with( m.id )”, I even get an error “undefined method `find’
    for #Class:0x7f2b3116a2c0” at the line “self.find( my_parent_id )”
    inside the method MyModel#my_parent.

Hope this clarified, it’s not real production code, but it models a
behavior sometimes I actually needed.

Saverio

On Sun, Jan 24, 2010 at 5:10 PM, Saverio M. [email protected]
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)

get :show, :id => m.id

Given the text you use in the example description (‘should display a
value for the children’) I wonder why you are passing the child’s id
into the show action. This seems off to me… are you actually
testing that the parent’s value is shown in the action?

end

In fact, since it looks like value is just an attribute of MyModel
here, I wonder why you would need to stub at all, when you can just
specify the value you are expecting when you create the m instance
in the test? It is hard to tell since you aren’t including any
verification in the example you gave. What is it you are actually
trying to test here? My best guess would be that you’re going for
something like this:

it “displays parent’s :value attribute” do
expected_value = 386438247
value_i_dont_care_about = 123
mp = MyModel.create!( :value => expected_value )
m = MyModel.create!( :my_parent_id => mp.id, :value =>
value_i_dont_care_about )
get “show”, :id => m.id
response.should include(expected_value)
end

The above tests that when you render a “show” for instance m that
m’s parent’s ‘value’ attribute shows up somewhere on the page.

What are you hoping for the stub to accomplish for you in this test?

what happens is:

  • if I don’t use “.with( m.id )”, @m_value and @mp_value will have
    m.value assigned.

This makes sense if you are indeed stubbing MyModel.find without
using with similar to my guess above, since any arguments you pass
would be ignored and the instance m would be returned in all cases.

  • if I use “.with( m.id )”, I even get an error “undefined method `find’
    for #Class:0x7f2b3116a2c0” at the line “self.find( my_parent_id )”
    inside the method MyModel#my_parent.

This is odd, and I’m not sure without more context why this would be
happening. My guess is that something else wasn’t wired up correctly
when you tried to do this.

Hope this clarified, it’s not real production code, but it models a
behavior sometimes I actually needed.

I think some more clarification is needed in order for us to properly
help you.

a) What is the exact behavior you are trying to test?
b) Why do you believe that a stub would help you test this behavior?

Hope this helps get us going in the right direction at least, :slight_smile:

Paul

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 H. wrote:

On Sun, Jan 24, 2010 at 5:10 PM, Saverio M. [email protected]
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)

Got at the bottom of it - as suggested,
object.stub(:method).with(:value).and_return(:ret) multiple times with
different :value [s], though it causes calls to :method to fail unless
it has one the :value [s] passed.

Thanks!
Saverio

Saverio M. wrote:

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