RSpec没有路由匹配 - 缺少必需的密钥:[:id]

时间:2017-03-02 19:20:26

标签: ruby-on-rails ruby-on-rails-4 rspec routes

由于没有路由匹配,我有一个失败的规范,这是相关的代码:

describe AdminController do
  before(:each) do
    @request.env["devise.mapping"] = Devise.mappings[:user]
    @admin = create(:admin)
    @user = create(:user)
    sign_in @admin, scope: :user
  end

  context "with admin user" do
    describe "DELETE #destroy" do
      it "deletes the contact" do
        puts @user.inspect
        expect{
          delete admin_destroy_user_path, id: @user.id
        }.to change(Profile, :count).by(-1)
      end

错误:

 AdminController with admin user DELETE #destroy deletes the contact
   Failure/Error: delete admin_destroy_user_path, id: @user.id

 ActionController::UrlGenerationError:
   No route matches {:action=>"destroy", :controller=>"admin"} missing    required keys: [:id]

路线:

admin_destroy_user DELETE /admin/:id(.:format)                              admin#destroy

我不确定我错过了什么,非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

感谢大家的帮助,我最终重构了使用Rails Admin。

答案 1 :(得分:0)

无论如何,您应该使用括号将ID传递给路线:

delete admin_destroy_user_path(id: @user.id)