多形性のコメントを作成しようとしていますが、rspecでルート一致のエラーが表示されません。Rspec no route polymorphicに一致します
class CommentsController < ApplicationController
before_action :authenticate_user!
def create
@comment = @commentable.comments.new(comment_params)
@comment.user_id = current_user.id
@comment.save
redirect_to :back, notice: "Your comment was successfully posted."
end
private
def comment_params
params.require(:comment).permit(:body)
end
end
はRSpecの私はテストのために、このアプローチを使用しています
describe "POST #create" do
context "with valid attributes" do
before do
@project = FactoryGirl.create(:project)
@comment_attr = FactoryGirl.build(:comment).attributes
end
it "creates a new comment" do
expect{
post :create, params: { project_id: @project, comment: @comment_attr }
}.to change(Comment, :count).by(1)
end
end
end
は、私の別のコントローラにアクションを作成し、そこに良いすべてがあるが、ここではいくつかの理由があるから、エラーがスローされます。私は私のエラーがどこに行為を作成するためにparamsを渡す行にあると思うが、私はエラーが表示されません。
UPDATE
resources :projects do
resources :comments, module: :projects
resources :tasks do
resources :comments, module: :tasks
end
end
UPDATE 2
Failure/Error: post :create, params: { project_id: @project, commentable: @project, comment: @comment_attr }
ActionController::UrlGenerationError: No route matches {:action=>"create", :comment=>{"id"=>nil, "commentable_type"=>nil, "commentable_id"=>nil, "user_id"=>nil, "body"=>"MyText", "created_at"=>nil, "updated_at"=>nil, "attachment"=>nil}, :commentable=>#, :controller=>"comments", :project_id=>#}
コントローラに '@ commentable'を設定していますか?また、 'routes.rb'を見れば、あなたが抜粋した残りのテストファイルに加えて助けになります。 –
@JimVanFleetさんが私の質問をルートで更新しました。 Commentableはプロジェクトまたはタスクですが、コメント可能なプロジェクトがプロジェクトの場合にテストしようとしています。 –
私はあなたの意図を理解していますが、上記の内容に基づいて '@ commentable'は' nil'です。それは 'ApplicationController'のヘルパーですか?テストログや500で404を取得していますか? –