gpt4 book ai didi

ruby-on-rails - Rails 4 + Rails-API + RSpec + Devise - 使用自定义设计路由对 Controller 进行故障测试

转载 作者:行者123 更新时间:2023-12-04 06:24:22 26 4
gpt4 key购买 nike

我目前正在构建 API 并尝试测试我的 Controller 。我收到无法解决的与 Devise 相关的错误。这是我目前拥有的:

# /spec/requests/api/v1/sessions_spec.rb
require 'spec_helper'

describe API::V1::SessionsController do
describe "POST /sessions" do
it "on success, returns the user's authentication token" do
user = FactoryGirl.create(:user)

user_params = {
"user" => {
"email" => user.email,
"password" => user.password
}
}.to_json

post "/api/v1/sessions", user_params

expect(response.status).to eq(200)
json = JSON.parse(response.body)
expect(json['authentication_token']).to eq(user.authentication_token)
end
end
end


# /app/controllers/api/v1/sessions_controller.rb
class API::V1::SessionsController < Devise::SessionsController
respond_to :json

def create
end
end


# routes.rb
MyApplication::Application.routes.draw do
namespace :api, defaults: { format: :json } do
namespace :v1 do
devise_scope :user do
post 'sessions' => 'sessions#create', :as => 'login'
end
end
end
end

我目前得到的错误是:

1) API::V1::SessionsController POST /sessions on success, returns the user's authentication token
Failure/Error: post "/api/v1/sessions", user_params
AbstractController::ActionNotFound:
Could not find devise mapping for path "/api/v1/sessions".
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/requests/api/v1/sessions_spec.rb:21:in `block (3 levels) in <top (required)>'

我四处寻找这个问题的解决方案,并尝试了很多不同的问题。此处问题的解决方案 ( How to write controller tests when you override devise registration controller? ) 表示将以下代码放在规范的 before block 中。

before :each do
request.env['devise.mapping'] = Devise.mappings[:user]
end

但是当我将它添加到我的 Controller 时,我收到另一个错误消息:

1) API::V1::SessionsController POST /sessions on success, returns the user's     authentication token
Failure/Error: request.env['devise.mapping'] = Devise.mappings[:user]
NoMethodError:
undefined method `env' for nil:NilClass
# ./spec/requests/api/v1/sessions_spec.rb:7:in `block (2 levels) in <top (required)>'

这里的另一个问题的解决方案 (undefined method 'env' for nil:NilClass) 说尝试将 include Devise::TestHelpers 添加到规范中,但是当我这样做时,返回相同的错误。

我目前使用的gems:

rails (4.0.2)

rails API (0.2.0)

rspec-rails (2.14.1)

设计(3.2.2)

使用这两个教程作为指南:https://lucatironi.github.io/tutorial/2012/10/15/ruby_rails_android_app_authentication_devise_tutorial_part_one/

http://commandercoriander.net/blog/2014/01/04/test-driving-a-json-api-in-rails/

任何帮助将不胜感激,谢谢!

最佳答案

我想我可能已经解决了这个问题。看来我必须在 api 命名空间 block 之外添加一个 devise_for :

MyApplication::Application.routes.draw do

devise_for :users, skip: [:sessions, :passwords, :registrations]

namespace :api, defaults: { format: :json } do
namespace :v1 do
devise_scope :user do
post 'sessions' => 'sessions#create', :as => 'login'
end
end
end
end

如果我在测试时遇到任何其他问题,我会将问题开放一段时间。

关于ruby-on-rails - Rails 4 + Rails-API + RSpec + Devise - 使用自定义设计路由对 Controller 进行故障测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23205370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com