我正在尝试测试创建用户。这是
registrations_controller_spec.rb
require 'rails_helper'
RSpec.describe DeviseTokenAuth::RegistrationsController, type: :controller do
before :each do
post :create, params:{email:"w@123.com",password:"123123",password_confirmation:"123123"}
@data = JSON.parse(response.body)
end
describe "Register a user" do
it "request should be successful" do
expect(response).to have_http_status(200)
end
end
end
当我进行测试时,我得到了:
Failures:
1) DeviseTokenAuth::RegistrationsController Register a user request should be successful
Failure/Error: post :create, params:{email:"w@123.com",password:"123123",password_confirmation:"123123"}
AbstractController::ActionNotFound:
Could not find devise mapping for path "/api/v1/auth".
This may happen for two reasons:
1) You forgot to wrap your route inside the scope block. For example:
devise_scope :user do
get "/some/route" => "some_devise_controller"
end
2) You are testing a Devise controller bypassing the router.
If so, you can explicitly tell Devise which mapping to use:
@request.env["devise.mapping"] = Devise.mappings[:user]
# ./spec/controllers/devise_token_auth/registrations_controller_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 0.01477 seconds (files took 1.76 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/controllers/devise_token_auth/registrations_controller_spec.rb:10 # DeviseTokenAuth::RegistrationsController Register a user request should be successful
该消息提供两种方式:
1)我试图将第一个添加到路线但不工作,我应该在哪里或如何添加它。
2)我在阻止之前添加了@request.env["devise.mapping"] = Devise.mappings[:user]
但不起作用。
我该如何解决这个问题?谢谢!
这是我的 routes.rb
Rails.application.routes.draw do
namespace :api do
scope :v1 do
mount_devise_token_auth_for 'User', at: 'auth'
end
end
end
答案 0 :(得分:3)
检查控制台:Devise.mappings
,这会给你一个提示。
在我的情况下,我不得不打电话
request.env['devise.mapping'] = Devise.mappings[:api_v1_user]
而不是
request.env["devise.mapping"] = Devise.mappings[:user]