0

In my users controller. I have

module Api;module V1
    class UsersController < ApplicationController
    skip_before_action :authenticate_user_from_token!, only: [:create]
    before_action :set_user, only: [:destroy, :update, :show]
    before_action :authorize_user, except: :create  


    def create 
     #TODO : fix for simultaneous account and registration create    
      @user = User.new user_params
      @user.save!
      render json: @user,status: :ok and return
    end
    private    

     def set_user
       @user = User.find(params[:id])
     end

    def authorize_user
      unless set_user == current_user 
       render json:{message: "you are not authorized."}, status: 401
      end  
    end 

  end
end;end     

Now my problem is that in my rspec user contro

RSpec.describe UsersController, type: :controller do

it "can create user" do
   post :create, user: {email: "ad@ad.ad",username: "add", name: "dad",
                                                         password: "password", password_confirmation: "password",
                                                         account:{company: "caaad"}}, :formate =>:json

    puts response                                        
end

end But its giving following error to me

Failure/Error: render json: @user, status: :ok and return

     NoMethodError:
       undefined method `authenticate' for nil:NilClass

My code is quite simple test not much logics. Why its not working ? I even tried to add

config.include Devise::TestHelpers, :type => :controller

in spec_helper but still its not working. Please tell me what I am missing ?

Although controller code is correct because it is working with normal Curl request.

in my user model

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  after_create :update_access_token!

 def update_access_token!
    self.access_token = "#{self.id}:#{Devise.friendly_token}"
    save!
  end 

end

Adt
  • 495
  • 6
  • 19

1 Answers1

0

That the test is going through in the first place, makes me suspect that there is another UsersController within the application, as I would have expected your spec to look like this:

RSpec.describe Api::V1::UsersController, type: :controller do

  it "can create user" do
     post :create, user: {email: "ad@ad.ad",username: "add", name: "dad",
                                                         password: "password", password_confirmation: "password",
                                                         account:{company: "caaad"}}, :formate =>:json

      puts response                                        
  end
end

If that still results in the error, I'd advise that you check out this SO answer

Community
  • 1
  • 1
oreoluwa
  • 5,553
  • 2
  • 20
  • 27
  • Thanks, For answer. I was setting up module Api;module V1 before declaring Rspec.describe so it is hitting right UserController. I am editing my question. – Adt Jul 06 '16 at 13:34