2012-02-04 101 views
3

我有一個相匹配的用戶名這樣的約束路線:RSpec的測試自定義的用戶名路由失敗

controller :users, :path => '/:username', :as => :user, :constrain => { :username => /^_?[a-z]_?(?:[a-z0-9]_?)*$/i } do 
    # lots of nested routes go here 
end 

當我去寫這個RSpec的測試(與使用user_id正常人一樣),所有的測試都因爲它「無法找到路線」,即使它在服務器上正常工作也會失敗。

describe "for an invalid request" do 
    it "should render a 404 if an associated photo is not found" do 
    # give it a bad photo id 
    xhr :post, :destroy, :id => "999999", :photo_id => "999999", :username => @photo_owner.username 
    # not found 
    response.status.should == not_found 
    end 
end 

此測試切換到用戶名前工作正常,我用我的路線user_id時:

resources :users do 
    # nested routes 
end 

xhr :post, :destroy, :id => "999999", :photo_id => "999999", :user_id => @photo_owner.id 

那我做錯了什麼已經改變?

我的服務器控制檯顯示這意味着我應該在所有正確傳遞的參數:

Processing by TagsController#destroy as JS 
    Parameters: {"constrain"=>{"username"=>/^_?[a-z]_?(?:[a-z0-9]_?)*$/i}, "username"=>"rubynewb", "photo_id"=>"2004-the-title-of-the-photo-here", "id"=>"1797"} 

回答

2

使用:constraints => {...}在你的路由定義。

你傳遞一個參數太多......

"constrain"=>{"username"=>/^_?[a-z]_?(?:[a-z0-9]_?)*$/i} 

Rails不承認:constrain,因此它和它的內容一起作爲一個參數,而不是由Rails的路由器被處理過。

+0

謝謝!這樣一個簡單的錯誤! – ZeNewb 2012-02-10 16:17:04