【发布时间】:2017-07-22 16:36:24
【问题描述】:
我的规范由于没有路由匹配而失败,这里是相关代码:
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
我不确定我错过了什么,任何帮助将不胜感激!
【问题讨论】:
-
您使用的是哪个版本的 RSpec?您可能需要像这样传递 id:
delete admin_destroy_user_path, params: { id: @user.id }假设@user.id实际上返回一个值。 -
或者你可以使用
admin_destroy_user_path(@user.id) -
谢谢先生们,但是这些都不起作用,我使用 params: 选项得到了同样的错误。如果我使用 Igor 的选项,我会得到 "ActionController::UrlGenerationError: No route matches {:action=>"/admin/2", :controller=>"admin"}" 另外我使用的是 RSpec 3.5.4 版。
-
你试过了吗:
admin_destroy_user_path(id: @user.id)? -
感谢 Taryn,我通过安装 Rails Admin 解决了这个问题。现在我可以做我需要的了。
标签: ruby-on-rails ruby-on-rails-4 rspec routes