3

現在、API を作成して、コントローラーをテストしようとしています。解決できない 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)>'

私はこの問題の解決策を探し回り、さまざまな質問からたくさん試しました。ここでの質問の解決策 (デバイス登録コントローラーをオーバーライドするときにコントローラーテストを作成する方法は? ) は、以下のコードを仕様の before ブロックに入れると述べています。

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

しかし、それをコントローラーに追加すると、次のような別のエラーが表示されます。

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を示していますが、それを行うと、同じエラーが返されます。

私が使用している現在の宝石:

レール (4.0.2)

レール API (0.2.0)

rspec レール (2.14.1)

工夫する (3.2.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/

どんな助けでも大歓迎です、ありがとう!

4

2 に答える 2

7

私は問題を解決したかもしれないと思います。API名前空間ブロックの外側に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

テスト中に他の問題が発生した場合に備えて、質問をもう少し開いたままにします。

于 2014-04-21T21:12:22.150 に答える
1

交換

  request.env['devise.mapping'] = Devise.mappings[:user]

  @request.env['devise.mapping'] = Devise.mappings[:user]

エラーメッセージが具体的に使用することを示唆していることに注意してください:

 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]
于 2014-04-21T20:55:20.507 に答える