次のようなユーザー名に一致する制約されたルートがあります。
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"}