我知道有相同的问题有很多问题,但没有一个解决方案似乎适用于我的情况。无法找到控制器路径的设计映射
我已经定义了我自己的会话,注册和用户控制器,因为我使用的是带有令牌认证的API,但我似乎无法获得我的路由/作用域范围工作。
我在尝试运行我的会话控制器上的规格测试(从DeviseController继承)
Failure/Error: post :create, credentials
AbstractController::ActionNotFound:
Could not find devise mapping for path "/api/v1/sessions/create?user_login%5Bemail%5D=rosina%40russel.name&user_login%5Bpassword%5D=12345678".
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]
其实我已经做了基于这样我其他的答案这两个东西收到以下错误消息已经阅读,但我不认为它为我的具体情况正确完成。
这里是规格测试我试图运行
describe "POST #create" do
before(:each) do
@user = FactoryGirl.create :user
@user.skip_confirmation!
@request.env['devise.mapping'] = Devise.mappings[:user]
end
context "when the credentials are correct" do
before(:each) do
credentials = { user_login: { :email => @user.email, :password => "12345678"} }
post :create, credentials
end
it "returns the user record corresponding to the given credentials" do
@user.reload
user_response = JSON.parse(response.body, symbolize_names: true)
expect(user_response[:auth_token]).to eql @user.auth_token
end
it { should respond_with :created }
end
end
正如你所看到的,我指定什么它说,我还没有规定。
这里是我的routes.rb我的API的定义,它的命名空间前的片段:
namespace :api, defaults: { format: :json } do
namespace :v1 do
devise_for :users, skip: [:sessions, :registrations]
devise_scope :user do
post 'sessions', to: 'sessions#create'
delete 'sessions', to: 'sessions#destroy'
end
如果有人从这个看到任何东西,请让我知道,我很想来测试我的控制器...
是的,我的sessions_controller位于app/controllers/api/v1/sessions_controller.rb
并正确定义为class API::V1::SessionsController < DeviseController
我注意到,devise_for应该SET Devise.mapping但似乎没有在所有发生的事情!
它会帮助如果你包含实际的规格,你的运行不仅仅是在块 – MilesStanfield
@MilesStanfield之前,它是无关紧要的,它甚至不会运行它通过发布请求。测试甚至没有机会运行 – Riptyde4
它**可能无关紧要。但你要求我们帮助,所以让我们决定 – MilesStanfield