Stub a controller helper method in a template helper spec

牧云@^-^@ 提交于 2019-12-10 13:23:29

问题


My ApplicationController exposes a method (e.g. sort_direction) to the view templates by using helper_method :sort_direction. I then use this method in another method (e.g. sort_link) of a view helper (application_helper.rb).

When testing the sort_link method with RSpec (in application_helper_spec.rb) I have to stub sort_direction as the test seems to run complete independent from the controllers (and thereby by its to the view templates exposed methods).

Unfortunately I could not find out how to stub that sort_direction method of the controller. I always get "undefined method".

Here is what I tried so far (inside application_helper_spec.rb):

helper.stub(:sort_direction)
controller.stub(:sort_direction)
view.stub(:sort_direction)
self.stub(:sort_direction)

Any suggestions how I can stub that method?

Here the error I get:

NoMethodError:
       undefined method `sort_direction' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0xb641434c>

回答1:


David Chelimsky solved that problem here: http://groups.google.com/group/rspec/browse_thread/thread/cc44ca12c6816053

Simply call in the spec all methods on the helper object:

it "should work" do
   helper.stub(:sort_direction)
   helper.sort_link(...).should == ...
end


来源:https://stackoverflow.com/questions/5373724/stub-a-controller-helper-method-in-a-template-helper-spec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!