2012-02-04 100 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