Saverio Miroddi
2009-Nov-10 14:08 UTC
[rspec-users] 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 -- Posted via http://www.ruby-forum.com/.
On 10 Nov 2009, at 14:08, Saverio Miroddi wrote:> Is there a clean/simple way of stubbing the activerecord find() for a > single instance?MyModel.stub(:find).with(42).and_return(myModel)
Saverio Miroddi
2009-Dec-12 19:35 UTC
[rspec-users] Stub activerecord find given instance?
Tom Stuart wrote:> On 10 Nov 2009, at 14:08, Saverio Miroddi 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. -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2009-Dec-14 12:39 UTC
[rspec-users] Stub activerecord find given instance?
On Sat, Dec 12, 2009 at 1:35 PM, Saverio Miroddi <lists at ruby-forum.com> wrote:> Tom Stuart wrote: >> On 10 Nov 2009, at 14:08, Saverio Miroddi 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?
Saverio Miroddi
2010-Jan-13 00:58 UTC
[rspec-users] Stub activerecord find given instance?
David Chelimsky 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. -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2010-Jan-13 03:49 UTC
[rspec-users] Stub activerecord find given instance?
On Tue, Jan 12, 2010 at 6:58 PM, Saverio Miroddi <lists at ruby-forum.com>wrote:> David Chelimsky 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. >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.> The example is, as I said, completely meaningless, but would simplify > some tests in our suites. > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100112/d3fe4fdd/attachment.html>
Saverio Miroddi
2010-Jan-24 23:10 UTC
[rspec-users] Stub activerecord find given instance?
David Chelimsky 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 -- Posted via http://www.ruby-forum.com/.
On Sun, Jan 24, 2010 at 5:10 PM, Saverio Miroddi <lists at ruby-forum.com> wrote:> David Chelimsky 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. > > 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 > endHi 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.idGiven 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?> endIn 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, :) Paul
Saverio Miroddi
2010-Feb-05 14:10 UTC
[rspec-users] Stub activerecord find given instance?
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/.
Saverio Miroddi
2010-Feb-08 23:05 UTC
[rspec-users] Stub activerecord find given instance?
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 Miroddi 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-- Posted via http://www.ruby-forum.com/.