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