2008-11-11 53 views
5

「控制器」比方說,我有下面的代碼在application_helper.rb擷取「ACTION_NAME」或幫手規範

def do_something 
if action_name == 'index' 
    'do' 
else 
    'dont' 
end 
end 

這將做一些事情,如果所謂的指數操作中。

問:如何重寫application_helper_spec.rb中的幫助程序規範以模擬來自'index'操作的調用?

describe 'when called from "index" action' do 
    it 'should do' do 
    helper.do_something.should == 'do' # will always return 'dont' 
    end 
end 

describe 'when called from "other" action' do 
    it 'should do' do 
    helper.do_something.should == 'dont' 
    end 
end 

回答

7

您可以存根ACTION_NAME方法你想要的任何值:

describe 'when called from "index" action' do 
    before 
    helper.stub!(:action_name).and_return('index') 
    end 
    it 'should do' do 
    helper.do_something.should == 'do' 
    end 
end 

describe 'when called from "other" action' do 
    before 
    helper.stub!(:action_name).and_return('other') 
    end 
    it 'should do' do 
    helper.do_something.should == 'dont' 
    end 
end 
+0

存根! (:index_name).and_return('index'),not helper.stub!(:action_name).and_return('index') 感謝rsim :)在幫助器中,該死的錯過了嘗試, – edthix 2008-11-11 12:40:46