6

我通过使用向视图模板ApplicationController公开一个方法(例如) 。然后我在视图助手( )的另一个方法(例如 )中使用此方法。sort_directionhelper_method :sort_directionsort_linkapplication_helper.rb

sort_link使用 RSpec (in application_helper_spec.rb) 测试方法时,我必须存根sort_direction,因为测试似乎完全独立于控制器运行(因此通过它到视图模板公开的方法)。

不幸的是,我无法找到如何存根sort_direction控制器的该方法。我总是得到“未定义的方法”。

这是我迄今为止尝试过的(内部application_helper_spec.rb):

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

任何建议我可以如何存根该方法?

这是我得到的错误:

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

1 回答 1

6

David Chelimsky 在这里解决了这个问题:http ://groups.google.com/group/rspec/browse_thread/thread/cc44ca12c6816053

只需在规范中调用辅助对象上的所有方法:

it "should work" do
   helper.stub(:sort_direction)
   helper.sort_link(...).should == ...
end
于 2011-03-22T03:22:21.730 回答