2011-05-08 79 views
3

在這個輔助函數請看:如何存根在輔助功能測試

def show_welcome_banner? 
    (controller_name == 'competition' && action_name == 'index') || 
    (controller_name == 'submissions' && action_name == 'show') 
end 

它預計要定義的CONTROLLER_NAME和ACTION_NAME功能。

我想在我的RSpec匹配器使用此:

describe PageHelper do 
    it "should know when the welcome banner is to be shown" do 
    helper.stub!(:controller_name).and_return('index') 
    show_welcome_banner?.should == false 
    end 
end 

但是這不會真正發揮作用。

我怎麼能stub內輔助功能?也許使用instance_eval? 謝謝!

編輯, 試圖用

controller.stub!(:controller_name).and_return('index') 

,但得到

1) PageHelper should know when the welcome banner is to be shown 
    Failure/Error: show_welcome_banner?.should == false 
    NameError: 
     undefined local variable or method `controller_name' for #<RSpec::Core::ExampleGroup::Nested_1:0x1059a10b8> 
    # ./app/helpers/page_helper.rb:16:in `show_welcome_banner?' 
    # ./spec/helpers/page_helper_spec.rb:7 

助手放在規格/幫手/ page_helper_spec.rb ..

回答

4

你試過

stub!(:controller_name).and_return('index') 

似乎已經爲我工作:)

+0

是的,那有效!謝謝! – 2011-06-20 07:16:53

0

有你在你的Rspec中嘗試過這樣的事情?

controller.stub!(:controller_name).and_return('index') 

幫助者只是包含在ApplicationController中的模塊。我相信這個方法來自ActionController::Base

+0

不,見上面。 – 2011-05-08 19:41:50

+0

試用'@ controller'而不是'controller'。 – jangosteve 2011-05-11 06:00:25

+0

沒有成功。同樣的錯誤信息.. – 2011-05-12 15:02:00

0

RSpec的護欄3.5

allow(helper).to receive(:controller_name) { 'submissions' }