0

I am having problems trying to post as JSON to my controller action within my rspec test

RSpec.describe RegistrationsController, type: :controller do
  context 'Adding a Valid User' do
    it 'Returns Success Code and User object' do
     @params = { :user => {username: 'name', school: 'school'} }.to_json
     post :create, @params
    end
  end
end

At the moment I want to get a successful post request firing but am getting this error back all the time

Failure/Error: post :create, @params
 AbstractController::ActionNotFound:
   Could not find devise mapping for path "/lnf".

My routes are setup like so

Rails.application.routes.draw do
 constraints(subdomain: 'api') do
   devise_for :users, path: 'lnf', controllers: { registrations: "registrations" }

   devise_scope :user do
     post "/lnf" => 'registrations#create'
   end
 end
end

Rake routes outputs the following

Prefix Verb              URI Pattern     Controller#Action
user_registration POST   /lnf(.:format)  registrations#create {:subdomain=>"api"}
lnf POST                 /lnf(.:format)  registrations#create {:subdomain=>"api"}

So i have 2 declarations for the same action?

Could anyone shed some light on this please

Thanks

Richlewis
  • 15,070
  • 37
  • 122
  • 283
  • Try to specify `subdomain`: `post :create, x: 1, y: 2, subdomain: :api` – Maxim Mar 10 '15 at 13:36
  • getting the same error back..any other ideas? – Richlewis Mar 10 '15 at 13:47
  • I don't think you are supposed to use ```devise_for``` and ```devise_scope``` at the same time. http://stackoverflow.com/questions/3827011/devise-custom-routes-and-login-pages https://github.com/plataformatec/devise#configuring-routes – Jesus Castello Mar 10 '15 at 15:36

1 Answers1

0

Ok so after a painstaking few hours i have got my tests passing correctly and posting as json by doing the following

require 'rails_helper'

RSpec.describe RegistrationsController, type: :controller do
  before :each do
    request.env['devise.mapping'] = Devise.mappings[:user]
  end
 context 'Adding a Valid User' do
  it 'Returns Success Code and User object' do
   json = { user: { username: "richlewis14", school: "Baden Powell", email: "richlewis14@gmail.com", password: "Password1", password_confirmation: "Password1"}}.to_json
  post :create, json
  expect(response.code).to eq('201')
 end
end
end

My Routes are back to normal

Rails.application.routes.draw do
 constraints(subdomain: 'api') do
  devise_for :users, path: 'lnf', controllers: { registrations: "registrations" }
 end
end

And in my test environment I had to add

config.action_mailer.default_url_options = { host: 'localhost' }

The key here was

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

Not fully sure as yet to what it does, its next on my list to find out, but my tests are starting to run and pass

Richlewis
  • 15,070
  • 37
  • 122
  • 283