2011-07-12 24 views
8

對於確定側邊欄行爲的控制器,我需要調用多個url;側欄會根據它出現的頁面更改(a.o)。在rspec測試中獲取url或路徑(作爲字符串提供)

describe SidebarController do 
    before(:each) do 
    @sidebar = SidebarController.new 
    end 
    it 'should return :jobseekers for root_path' do 
    get(root_url) 
    @sidebar.section(root_path).should eq :jobseekers 
    end 
end 

然而,這失敗ActionController::RoutingError: No route matches {:controller=>"sidebar", :action=>"http://test.host/"}

我可以get的URL或路徑字符串?有沒有更智能的方法來設置request.數據,而不是獲取它?

回答

15

我可以得到一個URL或路徑作爲字符串?

不在控制器測試中。測試控制器實際上並未處理URL,它使用5種支持請求類型之一(get,post,put,head,delete)設置測試,然後調用相應的控制器操作。請參閱Guide to Testing Rails Applications

您在尋找的是RSpec條款中的集成測試或「request spec」。

7

這裏有一個解決辦法...如果你想做到這一點...不一定是最好的辦法,但辦法..這個工程在軌道上3.2 ...

module ActionController::TestCase::Behavior 
    #just parse the url and then call the regular get method 
    def get_path(path) 
    parsed_params = Rails.application.routes.recognize_path path 
    controller = parsed_params.delete(:controller) 
    action = parsed_params.delete(:action) 
    get(action, parsed_params) 
    end 
end 
相關問題