Pregunta

I'm currently building out an API and trying to test my controller. I am getting errors related to Devise that I cannot solve. Here is what I currently have:

# /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

The error am I current getting is:

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)>'

I have searched around for a solution to this issue, and tried a bunch from different questions. The question's solution here (How to write controller tests when you override devise registration controller?) said to put the below code in a before block in the spec.

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

But when I add that to my controller, I get another error that says:

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)>'

Another question's solution here (undefined method 'env' for nil:NilClass) says to try adding include Devise::TestHelpers to the spec but when I do that, the same error is returned.

The current gems I am using:

rails (4.0.2)

rails-api (0.2.0)

rspec-rails (2.14.1)

devise (3.2.2)

Using these two tutorials as sort of a guide: 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/

Any help would be appreciated, thanks!

¿Fue útil?

Solución

I think I may have solved the issue. It seems I had to add in a devise_for outside the api namespace blocks:

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

I will keep the question open for a bit longer in case I run into any other issues when testing.

Otros consejos

Replace

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

With

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

Notice the error message specifically suggests to use:

 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]
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top