RSpec测试自定义用户名路由失败

3

我有一个限制路由,匹配用户名的格式如下:

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"}
1个回答

2
在路由定义中使用:constraints => {...}。您传递了一个过多的参数...
"constrain"=>{"username"=>/^_?[a-z]_?(?:[a-z0-9]_?)*$/i}

Rails不识别:constrain,因此它及其内容被作为参数传递,而不是由Rails路由器处理。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接