2011-03-21 76 views
6

我的ApplicationController通過使用helper_method :sort_direction向視圖模板公開了一種方法(例如sort_direction)。然後,我用另一種方法(例如sort_link)查看幫助器(application_helper.rb)。在模板助手規範中存在控制器輔助方法

當測試sort_link方法使用RSpec(在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> 

回答

6

大衛赫利姆斯基這裏解決了這個問題:http://groups.google.com/group/rspec/browse_thread/thread/cc44ca12c6816053

在規範只需調用幫助對象上的所有方法:

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

這是現在用rspec 3更新:'allow(helper).to receive(:sort_direction)'http://www.relishapp.com/rspec/rspec-mocks/v/3-1/docs/basics/allowing-messages – 2014-12-16 14:22:06