Hi all, I''m in the process of creating rspecs for my helpers. One of the helpers in app/helpers/application_helper.rb looks like this: def page_name @page_name || "Define @page_name in #{controller.controller_name}::#{controller.action_name}" end The rspec is simply: it "should something" do page_name end Running ./script/spec spec/helpers/application_helper_spec.rb produces: F 1) NameError in ''ApplicationHelper hmmmm'' undefined local variable or method `controller'' for [RSpec example]:#<Class:0xb70de334> /path/trunk/config/../app/helpers/application_helper.rb:21:in `page_title'' ./spec/helpers/application_helper_spec.rb:6: ./script/spec:4: Finished in 0.024011 seconds 1 example, 1 failure The helper spec doesn''t know about "controller". Would I have to mock the controller? If so, how? If not, what would be the solution to writing a spec for this or any helper that might use "controller"? -- Rick rick.tessner at gmail.com
Does this work? ApplicationHelper.stub!(:controller).and_return mock(''controller'', :controller_name => ''foo'') On 7/24/07, Rick Tessner <rick.tessner at gmail.com> wrote:> Hi all, > > I''m in the process of creating rspecs for my helpers. One of the > helpers in app/helpers/application_helper.rb looks like this: > > def page_name > @page_name || "Define @page_name in > #{controller.controller_name}::#{controller.action_name}" > end > > The rspec is simply: > > it "should something" do > page_name > end > > Running ./script/spec spec/helpers/application_helper_spec.rb produces: > > F > > 1) > NameError in ''ApplicationHelper hmmmm'' > undefined local variable or method `controller'' for [RSpec > example]:#<Class:0xb70de334> > /path/trunk/config/../app/helpers/application_helper.rb:21:in > `page_title'' > ./spec/helpers/application_helper_spec.rb:6: > ./script/spec:4: > > Finished in 0.024011 seconds > > 1 example, 1 failure > > The helper spec doesn''t know about "controller". Would I have to mock > the controller? If so, how? If not, what would be the solution to > writing a spec for this or any helper that might use "controller"? > > -- > Rick > rick.tessner at gmail.com > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Hi, On Tue, 2007-24-07 at 15:26 -0700, Courtenay wrote:> Does this work? > > ApplicationHelper.stub!(:controller).and_return mock(''controller'', > :controller_name => ''foo'')Sadly, no. Same result: undefined local variable or method `controller'' for [RSpec example]: #<Class:0xb7149760> -- Rick rick.tessner at gmail.com
On 7/24/07, Rick Tessner <rick.tessner at gmail.com> wrote:> Hi, > > On Tue, 2007-24-07 at 15:26 -0700, Courtenay wrote: > > Does this work? > > > > ApplicationHelper.stub!(:controller).and_return mock(''controller'', > > :controller_name => ''foo'') > > Sadly, no. Same result: > > undefined local variable or method `controller'' for [RSpec > example]: #<Class:0xb7149760>specing helpers just includes the module in your spec. So you can cheat like describe "Application Helper" do helper_name :application def controller @controller end before(:each) do @controller = mock(''controller'') end it "should blah blah" do end end
Hi, This worked just fine. :) Thanks. On Tue, 2007-24-07 at 16:30 -0700, Courtenay wrote:> specing helpers just includes the module in your spec. > > So you can cheat like > > describe "Application Helper" do > helper_name :application > > def controller > @controller > end > > before(:each) do > @controller = mock(''controller'') > end > > it "should blah blah" do > end > > end > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-- Rick Tessner <ricktessner at canada.com>