ActionController::UrlGenerationError 在 rspec

ActionController::UrlGenerationError in rspec

在测试我的控制器操作时,我遇到了这个路由错误。我无法弄清楚我哪里做错了

路线

 get ':user_type', to: 'user_posts#index', param: :user_type, constraints: lambda { |req| ['abc', 'def'].include?(req.params["user_type"]) }, as: :custom_posts


规格:

  describe "GET #index: enabled" do
    it "returns http success" do
      get :index, params: {user_type: 'abc'}
      expect(response).to have_http_status(:success)
    end
  end

错误:

ActionController::UrlGenerationError:
       No route matches {:action=>"index", :controller=>"user_posts"}

此外,我在 github 上尝试了一种解决方案,方法是更改​​我的路线,但它对我不起作用

get ':user_type', to: 'user_posts#index', param: :user_type, constraints: {user_type: lambda { |req| ['abc', 'def'].include?(req.params["user_type"]) } }, as: :custom_posts

终于有了答案。这是详细信息discussion

describe "GET #index: enabled" do
    it "returns http success" do
      get :index, params: { user_type: 'abc', param: :user_type }     
      expect(response).to have_http_status(:success)
    end
  end