Bart Zonneveld
2009-Jan-20 13:44 UTC
[rspec-users] [rspec] Stubbing partials in view specs
Hey list, As a good BDDer I want to test my views in isolation. And as a good rails programmer, I separate views into partials when needed. So, when testing my views, I want to stub out rendering of partials in my views. I''m working on upgrading an app from rails 2.1.2 to 2.2.2, using the latest rspec and rspec-rails. I used to throw template.stub!(:render) in a before(:each) block and be done with it, but that doesn''t work anymore. I can understand why, but now I have to do something like template.stub!(:render).with (hash_including(:partial => anything)). Except for when I''m testing a partial, then I need to replace the anything with every partial I''m rendering in my partial. Is this the correct way, or is there perhaps something like template.stub_partials :only => [], :except => [] ? thanks, bartz
David Chelimsky
2009-Jan-20 14:29 UTC
[rspec-users] [rspec] Stubbing partials in view specs
On Tue, Jan 20, 2009 at 7:44 AM, Bart Zonneveld <zuperinfinite at gmail.com> wrote:> Hey list, > > As a good BDDer I want to test my views in isolation.Sort of. A *good* BDDer wants to *specify* views in isolation. Testing is for testers :)> And as a good rails > programmer, I separate views into partials when needed. So, when testing my > views, I want to stub out rendering of partials in my views. I''m working on > upgrading an app from rails 2.1.2 to 2.2.2, using the latest rspec and > rspec-rails. > > I used to throw template.stub!(:render) in a before(:each) block and be done > with itThat sounds kinda risky because you could be ignoring partials that get rendered that you don''t want to be rendered.>, but that doesn''t work anymore. I can understand why, but now I have > to do something like template.stub!(:render).with(hash_including(:partial => > anything)). Except for when I''m testing a partial, then I need to replace > the anything with every partial I''m rendering in my partial. > > Is this the correct way,Seems like the only way at the moment. Wouldn''t call it correct or incorrect.> or is there perhaps something like > template.stub_partials :only => [], :except => [] ?Nothing like this exists. Seems like a reasonable idea. Feel free to submit a feature request, or better yet, a patch to http://rspec.lighthouseapp.com Cheers, David> > thanks, > bartz > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Bart Zonneveld
2009-Jan-20 16:35 UTC
[rspec-users] [rspec] Stubbing partials in view specs
On 20-jan-2009, at 15:29, David Chelimsky wrote:> On Tue, Jan 20, 2009 at 7:44 AM, Bart Zonneveld > <zuperinfinite at gmail.com> wrote: >> Hey list, >> >> As a good BDDer I want to test my views in isolation. > > Sort of. A *good* BDDer wants to *specify* views in isolation. Testing > is for testers :)You''re right! I tend to talk a lot to non-programmers, and they get that glaze-in-the-distance look in their eyes, whenever I mention specifiy, spec''ing, or what have you :).>> And as a good rails >> programmer, I separate views into partials when needed. So, when >> testing my >> views, I want to stub out rendering of partials in my views. I''m >> working on >> upgrading an app from rails 2.1.2 to 2.2.2, using the latest rspec >> and >> rspec-rails. >> >> I used to throw template.stub!(:render) in a before(:each) block >> and be done >> with it > > That sounds kinda risky because you could be ignoring partials that > get rendered that you don''t want to be rendered.It is, most definately.>> , but that doesn''t work anymore. I can understand why, but now I have >> to do something like template.stub!(:render).with(hash_including >> (:partial => >> anything)). Except for when I''m testing a partial, then I need to >> replace >> the anything with every partial I''m rendering in my partial. >> >> Is this the correct way, > > Seems like the only way at the moment. Wouldn''t call it correct or > incorrect.I would call it ugly :). Not only do I have to remember the hash_including part, but also the anything (and not :anything). Conceptually, I like the template.stub!(:render). I render a template, on which I stub all the renders. Whether that''s risky or not is a different discussion.>> or is there perhaps something like >> template.stub_partials :only => [], :except => [] ? > > Nothing like this exists. Seems like a reasonable idea. Feel free to > submit a feature request, or better yet, a patch to > http://rspec.lighthouseapp.comWill do! cheers, bartz
Evgeny Bogdanov
2009-Mar-18 10:18 UTC
[rspec-users] [rspec] Stubbing partials in view specs
Hello, I have a problem similar to one mentioned in this post. I call a partial inside another partial. _mother.haml contains: render :partial => "children/child" In mother_spec.rb file I am trying to stub the render call. Here is a working version: ============ template.should_receive(:render) template.stub!(:render) render :partial => ''mother'' ===========I would prefer to specify that the partial I am stubbing is "children/ child", however the following code doesn''t work for me: ========== template.stub!(:render).with(hash_including(:partial => ''children/ child'')) template.expect_render(:partial => ''children/child'') render :partial => ''mother'' : Mock ''render_proxy'' expected :render with ({:partial=>"children/ child"}) once, but received it 0 times ==========Is it possible to stub the render call with specific partial name? Thank you in advance, Evgeny On Jan 20, 5:35?pm, Bart Zonneveld <zuperinfin... at gmail.com> wrote:> On 20-jan-2009, at 15:29, David Chelimsky wrote: > > > On Tue, Jan 20, 2009 at 7:44 AM, Bart Zonneveld ? > > <zuperinfin... at gmail.com> wrote: > >> Hey list, > > >> As a good BDDer I want to test my views in isolation. > > > Sort of. A *good* BDDer wants to *specify* views in isolation. Testing > > is for testers :) > > You''re right! I tend to talk a lot to non-programmers, and they get ? > that glaze-in-the-distance look in their eyes, whenever I mention ? > specifiy, spec''ing, or what have you :). > > >> And as a good rails > >> programmer, I separate views into partials when needed. So, when ? > >> testing my > >> views, I want tostubout rendering of partials in my views. I''m ? > >> working on > >> upgrading an app from rails 2.1.2 to 2.2.2, using the latest rspec ? > >> and > >> rspec-rails. > > >> I used to throw template.stub!(:render) in a before(:each) block ? > >> and be done > >> with it > > > That sounds kinda risky because you could be ignoring partials that > > get rendered that you don''t want to be rendered. > > It is, most definately. > > >> , but that doesn''t work anymore. I can understand why, but now I have > >> to do something like template.stub!(:render).with(hash_including > >> (:partial => > >> anything)). Except for when I''m testing a partial, then I need to ? > >> replace > >> the anything with every partial I''m rendering in my partial. > > >> Is this the correct way, > > > Seems like the only way at the moment. Wouldn''t call it correct or ? > > incorrect. > > I would call it ugly :). Not only do I have to remember the ? > hash_including part, but also the anything (and not :anything). > Conceptually, I like the template.stub!(:render). Irendera ? > template, on which Istuball the renders. Whether that''s risky or ? > not is a different discussion. > > >> or is there perhaps something like > >> template.stub_partials :only => [], :except => [] ? > > > Nothing like this exists. Seems like a reasonable idea. Feel free to > > submit a feature request, or better yet, a patch to > >http://rspec.lighthouseapp.com > > Will do! > > cheers, > bartz > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
Hey there Evgeny. My response is inline.> I call a partial inside another partial. > _mother.haml contains: > render :partial => "children/child"> In mother_spec.rb file I am trying to stub the render call. > Here is a working version: > ============> ? ?template.should_receive(:render) > ? ?template.stub!(:render) > > ? ?render :partial => ''mother'' > ===========If you set a method expectation on an object (IE: template.should_receive(:render) ), you don''t need to stub the method (IE: template.stub!(:render) isn''t needed).> I would prefer to specify that the partial I am stubbing is "children/ > child", > however the following code doesn''t work for me: > ==========> ? ?template.stub!(:render).with(hash_including(:partial => ''children/ > child'')) > ? ?template.expect_render(:partial => ''children/child'') > > ? ?render :partial => ''mother'' > > : Mock ''render_proxy'' expected :render with ({:partial=>"children/ > child"}) once, but received it 0 times > ==========> Is it possible to stub the render call with specific partial name?What I said above about method expectations and method stubs also applies here. You should remove the call to #stub! . However, I''d be inclined to use #should_receive rather than #expect_render : template.should_receive(:render).with :partial => ''children/child'' render :partial => ''mother'' I haven''t written many view specs though, so maybe we''re supposed to use #expect_render . I hope that helps. Cheers, Nick
On Wed, Mar 18, 2009 at 4:35 PM, Nick Hoffman <nick at deadorange.com> wrote:> Hey there Evgeny. My response is inline. > >> I call a partial inside another partial. >> _mother.haml contains: >> render :partial => "children/child" > >> In mother_spec.rb file I am trying to stub the render call. >> Here is a working version: >> ============>> ? ?template.should_receive(:render) >> ? ?template.stub!(:render) >> >> ? ?render :partial => ''mother'' >> ===========> > If you set a method expectation on an object (IE: > template.should_receive(:render) ), you don''t need to stub the method > (IE: template.stub!(:render) isn''t needed). > >> I would prefer to specify that the partial I am stubbing is "children/ >> child", >> however the following code doesn''t work for me: >> ==========>> ? ?template.stub!(:render).with(hash_including(:partial => ''children/ >> child'')) >> ? ?template.expect_render(:partial => ''children/child'') >> >> ? ?render :partial => ''mother'' >> >> : Mock ''render_proxy'' expected :render with ({:partial=>"children/ >> child"}) once, but received it 0 times >> ==========>> Is it possible to stub the render call with specific partial name? > > What I said above about method expectations and method stubs also > applies here. You should remove the call to #stub! . > > However, I''d be inclined to use #should_receive rather than #expect_render : > > template.should_receive(:render).with :partial => ''children/child'' > render :partial => ''mother'' > > I haven''t written many view specs though, so maybe we''re supposed to > use #expect_render .#expect_render is deprecated prior to 1.2 and it has been removed 1.2. You don''t want to use that. :)> > I hope that helps. Cheers, > Nick > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- Zach Dennis http://www.continuousthinking.com http://www.mutuallyhuman.com
On Wed, Mar 18, 2009 at 6:18 AM, Evgeny Bogdanov <evgeny.bogdanov at gmail.com> wrote:> Hello, > I have a problem similar to one mentioned in this post. > I call a partial inside another partial. > _mother.haml contains: > render :partial => "children/child" > > In mother_spec.rb file I am trying to stub the render call. > Here is a working version: > ============> ? ?template.should_receive(:render) > ? ?template.stub!(:render) > > ? ?render :partial => ''mother'' > ===========> I would prefer to specify that the partial I am stubbing is "children/ > child", > however the following code doesn''t work for me: > ==========> ? ?template.stub!(:render).with(hash_including(:partial => ''children/ > child'')) > ? ?template.expect_render(:partial => ''children/child'')What version of rspec are you using? #expect_render has been removed in rspec 1.2 and has been deprecated for a while before that so you won''t want to rely on that unless you''re using an old version of rspec.> > ? ?render :partial => ''mother'' > > : Mock ''render_proxy'' expected :render with ({:partial=>"children/ > child"}) once, but received it 0 times > ==========> Is it possible to stub the render call with specific partial name?You had it right, hash including should work: template.stub!(:render).with(hash_including(:partial => "children/child")) To stub all partials being rendered: template.stub!(:render).with(hash_including(:partial => anything)) HTH,> > Thank you in advance, > Evgeny > > On Jan 20, 5:35?pm, Bart Zonneveld <zuperinfin... at gmail.com> wrote: >> On 20-jan-2009, at 15:29, David Chelimsky wrote: >> >> > On Tue, Jan 20, 2009 at 7:44 AM, Bart Zonneveld >> > <zuperinfin... at gmail.com> wrote: >> >> Hey list, >> >> >> As a good BDDer I want to test my views in isolation. >> >> > Sort of. A *good* BDDer wants to *specify* views in isolation. Testing >> > is for testers :) >> >> You''re right! I tend to talk a lot to non-programmers, and they get >> that glaze-in-the-distance look in their eyes, whenever I mention >> specifiy, spec''ing, or what have you :). >> >> >> And as a good rails >> >> programmer, I separate views into partials when needed. So, when >> >> testing my >> >> views, I want tostubout rendering of partials in my views. I''m >> >> working on >> >> upgrading an app from rails 2.1.2 to 2.2.2, using the latest rspec >> >> and >> >> rspec-rails. >> >> >> I used to throw template.stub!(:render) in a before(:each) block >> >> and be done >> >> with it >> >> > That sounds kinda risky because you could be ignoring partials that >> > get rendered that you don''t want to be rendered. >> >> It is, most definately. >> >> >> , but that doesn''t work anymore. I can understand why, but now I have >> >> to do something like template.stub!(:render).with(hash_including >> >> (:partial => >> >> anything)). Except for when I''m testing a partial, then I need to >> >> replace >> >> the anything with every partial I''m rendering in my partial. >> >> >> Is this the correct way, >> >> > Seems like the only way at the moment. Wouldn''t call it correct or >> > incorrect. >> >> I would call it ugly :). Not only do I have to remember the >> hash_including part, but also the anything (and not :anything). >> Conceptually, I like the template.stub!(:render). Irendera >> template, on which Istuball the renders. Whether that''s risky or >> not is a different discussion. >> >> >> or is there perhaps something like >> >> template.stub_partials :only => [], :except => [] ? >> >> > Nothing like this exists. Seems like a reasonable idea. Feel free to >> > submit a feature request, or better yet, a patch to >> >http://rspec.lighthouseapp.com >> >> Will do! >> >> cheers, >> bartz >> >> _______________________________________________ >> rspec-users mailing list >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- Zach Dennis http://www.continuousthinking.com http://www.mutuallyhuman.com
Evgeny Bogdanov
2009-Mar-19 12:59 UTC
[rspec-users] [rspec] Stubbing partials in view specs
just upgraded to 1.2.0 the following code works now: template.stub!(:render).with(hash_including(:partial => "children/ child")) template.should_receive(:render).with(hash_including(:partial => "children/child")) The only question that is left for me is about Nick''s words: "If you set a method expectation on an object (IE: template.should_receive(:render) ), you don''t need to stub the method (IE: template.stub!(:render) isn''t needed)." Basically, there are two situation. 1) I want to check if a function was called and I want to execute the function code 2) I want to check if a function was called and I don''t want to execute the function code so I used stubbing. I thought that for the first scenario the solution would be: template.should_receive(:render) #only checks if render function was called and for the second scenario template.stub!(:render) #stub render function template.should_receive(:render) #check if render function was called Am I right? Thanks, Evgeny On Mar 18, 11:23?pm, Zach Dennis <zach.den... at gmail.com> wrote:> On Wed, Mar 18, 2009 at 6:18 AM, Evgeny Bogdanov > > > > <evgeny.bogda... at gmail.com> wrote: > > Hello, > > I have a problem similar to one mentioned in this post. > > I call a partial inside another partial. > > _mother.haml contains: > > render :partial => "children/child" > > > In mother_spec.rb file I am trying to stub the render call. > > Here is a working version: > > ============> > ? ?template.should_receive(:render) > > ? ?template.stub!(:render) > > > ? ?render :partial => ''mother'' > > ===========> > I would prefer to specify that the partial I am stubbing is "children/ > > child", > > however the following code doesn''t work for me: > > ==========> > ? ?template.stub!(:render).with(hash_including(:partial => ''children/ > > child'')) > > ? ?template.expect_render(:partial => ''children/child'') > > What version of rspec are you using? #expect_render has been removed > in rspec 1.2 and has been deprecated for a while before that so you > won''t want to rely on that unless you''re using an old version of > rspec. > > > > > ? ?render :partial => ''mother'' > > > : Mock ''render_proxy'' expected :render with ({:partial=>"children/ > > child"}) once, but received it 0 times > > ==========> > Is it possible to stub the render call with specific partial name? > > You had it right, hash including should work: > ? ?template.stub!(:render).with(hash_including(:partial => "children/child")) > > To stub all partials being rendered: > ? ?template.stub!(:render).with(hash_including(:partial => anything)) > > HTH, > > > > > > > Thank you in advance, > > Evgeny > > > On Jan 20, 5:35?pm, Bart Zonneveld <zuperinfin... at gmail.com> wrote: > >> On 20-jan-2009, at 15:29, David Chelimsky wrote: > > >> > On Tue, Jan 20, 2009 at 7:44 AM, Bart Zonneveld > >> > <zuperinfin... at gmail.com> wrote: > >> >> Hey list, > > >> >> As a good BDDer I want to test my views in isolation. > > >> > Sort of. A *good* BDDer wants to *specify* views in isolation. Testing > >> > is for testers :) > > >> You''re right! I tend to talk a lot to non-programmers, and they get > >> that glaze-in-the-distance look in their eyes, whenever I mention > >> specifiy, spec''ing, or what have you :). > > >> >> And as a good rails > >> >> programmer, I separate views into partials when needed. So, when > >> >> testing my > >> >> views, I want tostubout rendering of partials in my views. I''m > >> >> working on > >> >> upgrading an app from rails 2.1.2 to 2.2.2, using the latest rspec > >> >> and > >> >> rspec-rails. > > >> >> I used to throw template.stub!(:render) in a before(:each) block > >> >> and be done > >> >> with it > > >> > That sounds kinda risky because you could be ignoring partials that > >> > get rendered that you don''t want to be rendered. > > >> It is, most definately. > > >> >> , but that doesn''t work anymore. I can understand why, but now I have > >> >> to do something like template.stub!(:render).with(hash_including > >> >> (:partial => > >> >> anything)). Except for when I''m testing a partial, then I need to > >> >> replace > >> >> the anything with every partial I''m rendering in my partial. > > >> >> Is this the correct way, > > >> > Seems like the only way at the moment. Wouldn''t call it correct or > >> > incorrect. > > >> I would call it ugly :). Not only do I have to remember the > >> hash_including part, but also the anything (and not :anything). > >> Conceptually, I like the template.stub!(:render). Irendera > >> template, on which Istuball the renders. Whether that''s risky or > >> not is a different discussion. > > >> >> or is there perhaps something like > >> >> template.stub_partials :only => [], :except => [] ? > > >> > Nothing like this exists. Seems like a reasonable idea. Feel free to > >> > submit a feature request, or better yet, a patch to > >> >http://rspec.lighthouseapp.com > > >> Will do! > > >> cheers, > >> bartz > > >> _______________________________________________ > >> rspec-users mailing list > >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > -- > Zach Dennishttp://www.continuousthinking.comhttp://www.mutuallyhuman.com > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
David Chelimsky
2009-Mar-19 14:30 UTC
[rspec-users] [rspec] Stubbing partials in view specs
On Mar 19, 2009, at 7:59 AM, Evgeny Bogdanov <evgeny.bogdanov at gmail.com> wrote:> just upgraded to 1.2.0 > the following code works now: > > template.stub!(:render).with(hash_including(:partial => "children/ > child")) > template.should_receive(:render).with(hash_including(:partial => > "children/child")) > > The only question that is left for me is about Nick''s words: > "If you set a method expectation on an object (IE: > template.should_receive(:render) ), you don''t need to stub the method > (IE: template.stub!(:render) isn''t needed)." > > Basically, there are two situation. > 1) I want to check if a function was called and I want to execute the > function code > 2) I want to check if a function was called and I don''t want to > execute the function code so I used stubbing. > > I thought that for the first scenario the solution would be: > template.should_receive(:render) #only checks if render function was > called > > and for the second scenario > template.stub!(:render) #stub render function > template.should_receive(:render) #check if render function was called > > Am I right?No. What I think you''re describing is called a test spy: a means of monitoring interactions without changing the behavior. RSpec''s mocks don''t provide this. The only ruby framework I''m aware of that does is RR. There may be others. A stub (stubs!) overrides an existing method (if one exists) and returns either self or any value you define with and_returns. A message expectation, or mocked method (should_receive) does the same thing *plus* it verifies that the message was called. HTH, David> > > Thanks, > Evgeny > > On Mar 18, 11:23 pm, Zach Dennis <zach.den... at gmail.com> wrote: >> On Wed, Mar 18, 2009 at 6:18 AM, Evgeny Bogdanov >> >> >> >> <evgeny.bogda... at gmail.com> wrote: >>> Hello, >>> I have a problem similar to one mentioned in this post. >>> I call a partial inside another partial. >>> _mother.haml contains: >>> render :partial => "children/child" >> >>> In mother_spec.rb file I am trying to stub the render call. >>> Here is a working version: >>> ============>>> template.should_receive(:render) >>> template.stub!(:render) >> >>> render :partial => ''mother'' >>> ===========>>> I would prefer to specify that the partial I am stubbing is >>> "children/ >>> child", >>> however the following code doesn''t work for me: >>> ==========>>> template.stub!(:render).with(hash_including(:partial => >>> ''children/ >>> child'')) >>> template.expect_render(:partial => ''children/child'') >> >> What version of rspec are you using? #expect_render has been removed >> in rspec 1.2 and has been deprecated for a while before that so you >> won''t want to rely on that unless you''re using an old version of >> rspec. >> >> >> >>> render :partial => ''mother'' >> >>> : Mock ''render_proxy'' expected :render with ({:partial=>"children/ >>> child"}) once, but received it 0 times >>> ==========>>> Is it possible to stub the render call with specific partial name? >> >> You had it right, hash including should work: >> template.stub!(:render).with(hash_including(:partial => >> "children/child")) >> >> To stub all partials being rendered: >> template.stub!(:render).with(hash_including(:partial => anything)) >> >> HTH, >> >> >> >> >> >>> Thank you in advance, >>> Evgeny >> >>> On Jan 20, 5:35 pm, Bart Zonneveld <zuperinfin... at gmail.com> wrote: >>>> On 20-jan-2009, at 15:29, David Chelimsky wrote: >> >>>>> On Tue, Jan 20, 2009 at 7:44 AM, Bart Zonneveld >>>>> <zuperinfin... at gmail.com> wrote: >>>>>> Hey list, >> >>>>>> As a good BDDer I want to test my views in isolation. >> >>>>> Sort of. A *good* BDDer wants to *specify* views in isolation. >>>>> Testing >>>>> is for testers :) >> >>>> You''re right! I tend to talk a lot to non-programmers, and they get >>>> that glaze-in-the-distance look in their eyes, whenever I mention >>>> specifiy, spec''ing, or what have you :). >> >>>>>> And as a good rails >>>>>> programmer, I separate views into partials when needed. So, when >>>>>> testing my >>>>>> views, I want tostubout rendering of partials in my views. I''m >>>>>> working on >>>>>> upgrading an app from rails 2.1.2 to 2.2.2, using the latest >>>>>> rspec >>>>>> and >>>>>> rspec-rails. >> >>>>>> I used to throw template.stub!(:render) in a before(:each) block >>>>>> and be done >>>>>> with it >> >>>>> That sounds kinda risky because you could be ignoring partials >>>>> that >>>>> get rendered that you don''t want to be rendered. >> >>>> It is, most definately. >> >>>>>> , but that doesn''t work anymore. I can understand why, but now >>>>>> I have >>>>>> to do something like template.stub!(:render).with(hash_including >>>>>> (:partial => >>>>>> anything)). Except for when I''m testing a partial, then I need to >>>>>> replace >>>>>> the anything with every partial I''m rendering in my partial. >> >>>>>> Is this the correct way, >> >>>>> Seems like the only way at the moment. Wouldn''t call it correct or >>>>> incorrect. >> >>>> I would call it ugly :). Not only do I have to remember the >>>> hash_including part, but also the anything (and not :anything). >>>> Conceptually, I like the template.stub!(:render). Irendera >>>> template, on which Istuball the renders. Whether that''s risky or >>>> not is a different discussion. >> >>>>>> or is there perhaps something like >>>>>> template.stub_partials :only => [], :except => [] ? >> >>>>> Nothing like this exists. Seems like a reasonable idea. Feel >>>>> free to >>>>> submit a feature request, or better yet, a patch to >>>>> http://rspec.lighthouseapp.com >> >>>> Will do! >> >>>> cheers, >>>> bartz >> >>>> _______________________________________________ >>>> rspec-users mailing list >>>> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/ >>>> rspec-users >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-us... at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >> >> -- >> Zach Dennishttp://www.continuousthinking.comhttp://www.mutuallyhuman.com >> _______________________________________________ >> rspec-users mailing list >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/ >> rspec-users > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
> Basically, there are two situation. > 1) I want to check if a function was called and I want to execute the > function codeI don''t think that''s possible. Setting a method expectation (Eg: template.should_receive(:render)) automatically stubs #render.> 2) I want to check if a function was called and I don''t want to > execute the function code so I used stubbing.That''s how #should_receive works.> I thought that for the first scenario the solution would be: > template.should_receive(:render) #only checks if render function was > calledYou''re correct that the only thing that''s checked is whether or not #render was called on the "template" object. However, creating that expectation causes RSpec to stub #render . If you want to confirm this, call #render , and you''ll see that it returns nil. nil is the default return value for a stubbed method.> and for the second scenario > template.stub!(:render) #stub render function > template.should_receive(:render) #check if render function was calledMostly! All you need is the call to #should_receive . The call to #stub! doesn''t do anything bad...it just doesn''t do anything at all. Cheers, Nick
Scott Taylor
2009-Mar-19 15:39 UTC
[rspec-users] [rspec] Stubbing partials in view specs / Test Spy
On Mar 19, 2009, at 10:30 AM, David Chelimsky wrote:> On Mar 19, 2009, at 7:59 AM, Evgeny Bogdanov <evgeny.bogdanov at gmail.com > > wrote: > >> just upgraded to 1.2.0 >> the following code works now: >> >> template.stub!(:render).with(hash_including(:partial => "children/ >> child")) >> template.should_receive(:render).with(hash_including(:partial => >> "children/child")) >> >> The only question that is left for me is about Nick''s words: >> "If you set a method expectation on an object (IE: >> template.should_receive(:render) ), you don''t need to stub the method >> (IE: template.stub!(:render) isn''t needed)." >> >> Basically, there are two situation. >> 1) I want to check if a function was called and I want to execute the >> function code >> 2) I want to check if a function was called and I don''t want to >> execute the function code so I used stubbing. >> >> I thought that for the first scenario the solution would be: >> template.should_receive(:render) #only checks if render function was >> called >> >> and for the second scenario >> template.stub!(:render) #stub render function >> template.should_receive(:render) #check if render function was called >> >> Am I right? > > No. What I think you''re describing is called a test spy: a means of > monitoring interactions without changing the behavior. RSpec''s mocks > don''t provide this. The only ruby framework I''m aware of that does > is RR. There may be others. >"Before we exercise the SUT, we install a Test Spy as a stand-in for depended-on component (DOC) used by the SUT. The Test Spy is designed to act as an observation point by recording the method calls made to it by the SUT as it is exercised. During the result verification phase, the test compares the actual values passed to the Test Spy by the SUT with the values expected by the test. " http://xunitpatterns.com/Test%20Spy.html Actually, the "Not A Mock" framework also supports the test spy pattern: http://github.com/notahat/not_a_mock/tree/master Scott> A stub (stubs!) overrides an existing method (if one exists) and > returns either self or any value you define with and_returns. > > A message expectation, or mocked method (should_receive) does the > same thing *plus* it verifies that the message was called. > > > HTH, > David > >> >> >> Thanks, >> Evgeny >> >> On Mar 18, 11:23 pm, Zach Dennis <zach.den... at gmail.com> wrote: >>> On Wed, Mar 18, 2009 at 6:18 AM, Evgeny Bogdanov >>> >>> >>> >>> <evgeny.bogda... at gmail.com> wrote: >>>> Hello, >>>> I have a problem similar to one mentioned in this post. >>>> I call a partial inside another partial. >>>> _mother.haml contains: >>>> render :partial => "children/child" >>> >>>> In mother_spec.rb file I am trying to stub the render call. >>>> Here is a working version: >>>> ============>>>> template.should_receive(:render) >>>> template.stub!(:render) >>> >>>> render :partial => ''mother'' >>>> ===========>>>> I would prefer to specify that the partial I am stubbing is >>>> "children/ >>>> child", >>>> however the following code doesn''t work for me: >>>> ==========>>>> template.stub!(:render).with(hash_including(:partial => >>>> ''children/ >>>> child'')) >>>> template.expect_render(:partial => ''children/child'') >>> >>> What version of rspec are you using? #expect_render has been removed >>> in rspec 1.2 and has been deprecated for a while before that so you >>> won''t want to rely on that unless you''re using an old version of >>> rspec. >>> >>> >>> >>>> render :partial => ''mother'' >>> >>>> : Mock ''render_proxy'' expected :render with ({:partial=>"children/ >>>> child"}) once, but received it 0 times >>>> ==========>>>> Is it possible to stub the render call with specific partial name? >>> >>> You had it right, hash including should work: >>> template.stub!(:render).with(hash_including(:partial => >>> "children/child")) >>> >>> To stub all partials being rendered: >>> template.stub!(:render).with(hash_including(:partial => anything)) >>> >>> HTH, >>> >>> >>> >>> >>> >>>> Thank you in advance, >>>> Evgeny >>> >>>> On Jan 20, 5:35 pm, Bart Zonneveld <zuperinfin... at gmail.com> wrote: >>>>> On 20-jan-2009, at 15:29, David Chelimsky wrote: >>> >>>>>> On Tue, Jan 20, 2009 at 7:44 AM, Bart Zonneveld >>>>>> <zuperinfin... at gmail.com> wrote: >>>>>>> Hey list, >>> >>>>>>> As a good BDDer I want to test my views in isolation. >>> >>>>>> Sort of. A *good* BDDer wants to *specify* views in isolation. >>>>>> Testing >>>>>> is for testers :) >>> >>>>> You''re right! I tend to talk a lot to non-programmers, and they >>>>> get >>>>> that glaze-in-the-distance look in their eyes, whenever I mention >>>>> specifiy, spec''ing, or what have you :). >>> >>>>>>> And as a good rails >>>>>>> programmer, I separate views into partials when needed. So, when >>>>>>> testing my >>>>>>> views, I want tostubout rendering of partials in my views. I''m >>>>>>> working on >>>>>>> upgrading an app from rails 2.1.2 to 2.2.2, using the latest >>>>>>> rspec >>>>>>> and >>>>>>> rspec-rails. >>> >>>>>>> I used to throw template.stub!(:render) in a before(:each) block >>>>>>> and be done >>>>>>> with it >>> >>>>>> That sounds kinda risky because you could be ignoring partials >>>>>> that >>>>>> get rendered that you don''t want to be rendered. >>> >>>>> It is, most definately. >>> >>>>>>> , but that doesn''t work anymore. I can understand why, but now >>>>>>> I have >>>>>>> to do something like template.stub!(:render).with(hash_including >>>>>>> (:partial => >>>>>>> anything)). Except for when I''m testing a partial, then I need >>>>>>> to >>>>>>> replace >>>>>>> the anything with every partial I''m rendering in my partial. >>> >>>>>>> Is this the correct way, >>> >>>>>> Seems like the only way at the moment. Wouldn''t call it correct >>>>>> or >>>>>> incorrect. >>> >>>>> I would call it ugly :). Not only do I have to remember the >>>>> hash_including part, but also the anything (and not :anything). >>>>> Conceptually, I like the template.stub!(:render). Irendera >>>>> template, on which Istuball the renders. Whether that''s risky or >>>>> not is a different discussion. >>> >>>>>>> or is there perhaps something like >>>>>>> template.stub_partials :only => [], :except => [] ? >>> >>>>>> Nothing like this exists. Seems like a reasonable idea. Feel >>>>>> free to >>>>>> submit a feature request, or better yet, a patch to >>>>>> http://rspec.lighthouseapp.com >>> >>>>> Will do! >>> >>>>> cheers, >>>>> bartz >>> >>>>> _______________________________________________ >>>>> rspec-users mailing list >>>>> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users >>>> _______________________________________________ >>>> rspec-users mailing list >>>> rspec-us... at rubyforge.org >>>> http://rubyforge.org/mailman/listinfo/rspec-users >>> >>> -- >>> Zach Dennishttp://www.continuousthinking.comhttp://www.mutuallyhuman.com >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users