Ruby on rails ActionController::UrlGenerationError:没有路由匹配(Rspec)

Ruby on rails ActionController::UrlGenerationError:没有路由匹配(Rspec),ruby-on-rails,rspec,Ruby On Rails,Rspec,使用RSpec测试我的commentscoontroller时出现以下错误: ActionController::UrlGenerationError: No route matches {:action=>"create", :comment=>{:comment=>"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}

使用
RSpec
测试我的
commentscoontroller
时出现以下错误:

ActionController::UrlGenerationError: 
  No route matches {:action=>"create", :comment=>{:comment=>"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, :controller=>"comments"}
规格/型号/注释\u规格rb

RSpec.describe CommentsController, type: :controller do
  let!(:user) { create(:user) }
  let!(:post1) { create(:post, user: user) }
  let!(:comment) { create(:comment, user_id: user.id, post_id: post1.id) }
  let!(:comment_attributes) { attributes_for(:comment) }

  describe "#create" do
    before do
      sign_in user
    end

    it 'save post' do
      expect do
        post :create, params: { comment: comment_attributes }, session: {}
      end.to change(Comment, :count).by(1)
    end

    it 'if post saves, redirect_to posts page' do
      post :create, params: { post: comment_attributes }, session: {}
      expect(response).to redirect_to(posts_path)
    end
  end
end

每次创建新资源(您希望通过链接访问)时,都必须更新
routes.rb
文件,否则Rails不知道在给定URL中使用哪个控制器。添加
resources:comments
行在
routes.rb
中应该为您做这件事。

spec/models/comment\u spec.rb
对于控制器规范,您应该将规范文件放在控制器目录中。例如:
spec/controllers/comments\u controller\u spec.rb
。这样,RSpec将能够自动确定正在执行的测试类型,并且您可以从
descripe
方法中删除
type::controller