我有一个限制路由,匹配用户名的格式如下:
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"}