Hello, Does someone know where I can hook in to mock out a render call from a helper in Rails? I know I can do this in view specs with the following @controller.template.stub!(:render) but template doesn''t seem to be attached to @controller in a helper context. Thanks. -Chris
Okay, so if I use @controller.render :foo instead of just render :foo in the helper, I can mock it out fine in the specs with @controller.stub!(:render) Am I wrong to use the shortened "render :foo" form in the helpers, or is this a malfunction of RSpec? On 4/13/07, Chris Hoffman <bosshoff at gmail.com> wrote:> Hello, > Does someone know where I can hook in to mock out a render call from > a helper in Rails? I know I can do this in view specs with the > following > > @controller.template.stub!(:render) > > but template doesn''t seem to be attached to @controller in a helper > context. Thanks. > > -Chris >
I guess this question really boils down to the following: what are the proper means to access the object that is including the helper being tested? I have been reading through the documentation, and it indicates that calling helper_name :foo will include FooHelper in the context. How then can I mock out methods in this object? In case I am not being clear: say I have the following helper module FooHelper def hello "hello world" end def look_to_hello hello end end I want to make sure the "look_to_hello" method calls "hello," without "hello" actually being called. Thanks. On 4/13/07, Chris Hoffman <bosshoff at gmail.com> wrote:> Okay, so if I use > > @controller.render :foo > > instead of just > > render :foo > > in the helper, I can mock it out fine in the specs with > > @controller.stub!(:render) > > Am I wrong to use the shortened "render :foo" form in the helpers, or > is this a malfunction of RSpec? > > On 4/13/07, Chris Hoffman <bosshoff at gmail.com> wrote: > > Hello, > > Does someone know where I can hook in to mock out a render call from > > a helper in Rails? I know I can do this in view specs with the > > following > > > > @controller.template.stub!(:render) > > > > but template doesn''t seem to be attached to @controller in a helper > > context. Thanks. > > > > -Chris > > >
Sorry for this stream of consciousness. I am trying to work through a problem, and without the benefit of being able to discuss these situations with my colleague (who is not with the department any longer), I find it necessary to reason through my conundrums by writing. Anyway, I think I have discovered the proper means to achieve what I was talking about in previous posts. ''self'' in the spec seems to give one access to the object that is including the helper being spec''d. -Chris On 4/13/07, Chris Hoffman <bosshoff at gmail.com> wrote:> I guess this question really boils down to the following: what are > the proper means to access the object that is including the helper > being tested? I have been reading through the documentation, and it > indicates that calling helper_name :foo will include FooHelper in the > context. How then can I mock out methods in this object? > > In case I am not being clear: say I have the following helper > > module FooHelper > def hello > "hello world" > end > > def look_to_hello > hello > end > end > > I want to make sure the "look_to_hello" method calls "hello," without > "hello" actually being called. > > Thanks. > > On 4/13/07, Chris Hoffman <bosshoff at gmail.com> wrote: > > Okay, so if I use > > > > @controller.render :foo > > > > instead of just > > > > render :foo > > > > in the helper, I can mock it out fine in the specs with > > > > @controller.stub!(:render) > > > > Am I wrong to use the shortened "render :foo" form in the helpers, or > > is this a malfunction of RSpec? > > > > On 4/13/07, Chris Hoffman <bosshoff at gmail.com> wrote: > > > Hello, > > > Does someone know where I can hook in to mock out a render call from > > > a helper in Rails? I know I can do this in view specs with the > > > following > > > > > > @controller.template.stub!(:render) > > > > > > but template doesn''t seem to be attached to @controller in a helper > > > context. Thanks. > > > > > > -Chris > > > > > >
On 13 Apr 2007, at 14:54, Chris Hoffman wrote:> module FooHelper > def hello > "hello world" > end > > def look_to_hello > hello > end > endChris, The approach I''ve taken is to turn the support methods into methods on separate objects, eg (not that I''ve had this exact problem) module FooHelper class Greeter def hello "hello world" end end def look_to_hello Greeter.new.hello end end You could make it "hello" class method if you''re not bothered about blanket stubbing. (I tend to go with objects just in case I want to inject them later, rather than have them created in-place with new) Ashley