2012-12-18 44 views
7

我有以下rspec路由規範,但我需要在帖子中指定:defaults => { :format => 'json' };我將如何做到這一點?在路由規範中指定默認格式

規格:

it "should route to all-locations-nav" do 
    {:post => locations_nav_path }.should route_to(:controller => "api", :action => "locations_nav") 
end 

編輯#1
如此玩弄,它看起來像這樣修復它:

it "should route to all-locations-nav" do 
    {:post => locations_nav_path }.should route_to(:controller => "api", :action => "locations_nav", :format => "json") 
end 

但如果這是記錄任何地方感到好奇?

回答

4

只需設置在這樣的規範格式...

it "routes to #create" do 
    expect(:post => "/post").to route_to("posts#create", :format => :json) 
end 

龍解釋...

你看到的行爲是不特定於:format,相反卻是一個您在rake routes中看到的符號與您傳遞給route_to的符號之間的關係。

例如,假設你上面的例子,我期望像下面這樣當您運行rake routes

locations_nav POST /api/locations_nav(.:format) api#locations_nav

:controller:action沒有明確的rake routes響應標記,那些內置於Rails的MVC結構中,但明確顯示了:format,並將:format改爲route_to。例如...

同樣,你可能會看到你的rake routes輸出,這將傳遞一個:id參數route_to加以利用一些:id引用。

有關在RSpec中路由的其他示例,請參見"rspec-rails" documentation

在內部,RSpec的route_to代表Rails的assert_recognizes,您可以在Rails documentation中看到記錄。